Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:54:17+00:00 2026-06-08T07:54:17+00:00

Here’s the situation. I’m building a site which uses lots of scripts looking somewhat

  • 0

Here’s the situation. I’m building a site which uses lots of scripts looking somewhat like this:

function getRandomArrayIndex(source_array) {

return Math.floor(Math.random() * source_array.length);

}

function getRandomArrayEntry(source_array) {

var random_index = getRandomArrayIndex(source_array);

return source_array[random_index];

}

function getRandomBlah() {

var blahs = [
["A"],
["B"],
["C"],
["D"],
["E"],
["F"],
["G"],
["H"],
["I"],
["L"],
["M"],
["N"],
["O"],
["P"],
["R"],
["S"],
["T"],
["V"],
["W"],
["Y"],
]; var random_blah = getRandomArrayEntry(blahs);

return random_blah;

}

function displayBlah(blah) {

const TEXT_ROW = 0;

const LINK_ROW = 1;

var blah_text = blah[TEXT_ROW];

var blah_link = blah[LINK_ROW]; if (blah_link != null) {

document.getElementById("blah").innerHTML = '<a href="' + blah_link + '">' + blah_text + '</a>';

} else {

document.getElementById("blah").innerHTML = blah_text;

}

}

function generateRandomBlah(){

var random_blah = getRandomBlah();

displayBlah(random_blah);

}    

And this will, when called with <body onload="generateRandomBlah()">, insert one of the letters at random into <span id="blah"></span>.

So there’s about 15 of these scripts, each with their own functions named slightly differently for different uses – generateRandomBlah2, etc, with a corresponding different place in the HTML for each script to do its work.

Because I’m not a very good coder, the way the whole thing works is that in the ‘body onload’ tag, there’s about 15 different ‘generateRandomBlah()’ functions just within this one tag. The nature of the site means that on any one page, I will only need 2 or 3 of these scripts at once, but I require the ability to call any of them on any page. As you can see, my current tactic is to just call them all at once, and if the corresponding doesn’t exist for a script, it’ll just ignore that fact and move onto the next one.

Except that it doesn’t ignore the fact that there’s no corresponding <span>.

As soon as one isn’t present, the rest of the scripts break and don’t actually do what they’re supposed to do. Looking at the code in Chrome’s ‘inspect code’ shows an error at the first script which happens to break: “Uncaught TypeError: Cannot set property ‘innerHTML’ of null”. I see a couple of potential solutions, but I might be completely off:

1) Add some code into each script which tells it that, if there’s no <span id> on the page to insert its code, it ends gracefully and moves onto the next one – gradually (obviously in less than a second speed-wise) going through the scripts and only running them if actually exists. (As you can see, the problem is that a script will get ‘snagged’ on the fact that there’s no place to insert its code and doesn’t just end gracefully if that happens.

2) Get rid of the ‘onload’ stuff and just make each script self-containing, calling its own function. I don’t know if this would fix the problem, though.

Anyway, some help would be much appreciated, as I’m stumped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:54:18+00:00Added an answer on June 8, 2026 at 7:54 am

    As you said in your first solution, testing for null before trying to “do” anything is probably your best bet short of completely recoding.

    function getRandomArrayIndex(source_array) {
      if(source_array === null) return; // similar lines in each function should fix everything
      return Math.floor(Math.random() * source_array.length);
    }
    

    That said, a much better approach than your current solution of making a bunch of very similar functions and running them all is to create a single function that can take parameters indicating what it should be doing. If they really are quite similar, it shouldn’t be difficult and will result in a lot fewer lines of code. It would also be nice if you can find a way for your page to not call all of the functions every time, though with fixing the null pointers that’s a much smaller issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here's a coding problem for those that like this kind of thing. Let's see
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here's what my table TheTable looks like ColA | ColB | ColC ------+-------+------ abc
Here's the situation: void funct( unsigned u, double d, float f ) { u
here is how i call the following function: List<decimal> numbers = new List<decimal>(); numbers.Add(210m);
Here is the setup: dynamically generates content for a number of items which need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.