• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

I want to randomly call up one of three javascript functions

So I have 3 links to different javascript functions--and want to create something that will randomly call one of them to run.

Basically something that would look like this:

random(33%, '<script language = javascript src = a.js></script>', 33%, '<script language = javascript src = b.js</script>', 33%, '<script language = javascript src = c.js></script>')

Are there any functions out there that can do this for me?
 
Originally posted by: Syringer
Pages are static.

Then use the DOM to add a new javascript file (this syntax might have errors, I hand wrote it for this post):

function addJavascript(jsFile) {
var headTag = document.getElementsByTagName("head")[0];
var newScript = document.createElement("script");
newScript.setAttribute("type", "text/javascript");
newScript.setAttribute("src", jsFile);
headTag.appendChild(newScript);
}

function chooseJSFile() {
// Random number 0 - 2 inclusive
var randNum = Math.floor(Math.random() * 3)
switch (randNum) {
case 0:
addJavascript("a.js");
break;
case 1:
addJavascript("b.js");
break;
default:
addJavascript("c.js");
}
}
 
Ahh, that looks great..however I'm a real noob when it comes to JS..how do I actually "run" the chooseJSFile function?

I have that code put into my html page, within a <script language = ....></script> section, where and how would I use chooseJSFile?

Thanks!
 
Originally posted by: Syringer
Ahh, that looks great..however I'm a real noob when it comes to JS..how do I actually "run" the chooseJSFile function?

I have that code put into my html page, within a <script language = ....></script> section, where and how would I use chooseJSFile?

Thanks!

You can add it to the body tag:

<body onremovethisLoad="chooseJSFile();">
 
You could also just put a literal call at the end of the script itself.


......
break;
default:
addJavascript("c.js");
}
chooseJSFile();
}
 
The way suggested above may not actually work, for several reasons. One being timing (too late in the game) - by the time the browser completes loading the body of the document and executes the body.onload event, the HEAD section has long been processed and rendered. Adding things to it will not process the instructions to load the js files. And the other reason is lack of actual function calls (that is actually what you wanted, calling functions randomly). Just by loading source files containing some functions, the execution of those will not magically begin. What you might want to do is to save all three functions into one js file. Then, permanently, instead of dynamically as it was suggested, add the line that links the source file to the HEAD section of your document. And, directly following the aforementioned line <script language = "javascript" src = "myfunctions.js"></script>, add this function, which is almost the same as suggested:
function chooseJSFunction() {
// Random number 0 - 2 inclusive
var randNum = Math.floor(Math.random() * 3)
switch (randNum) {
case 0:
FunctionOne();
break;
case 1:
FunctionTwo();
break;
default:
FunctionThree();
}
}
Where FunctionOne, FunctionTwo and FunctionThree are the exact names of the functions included inside the source js file you linked above. Afterwards, you can use the body.onload, as was suggested, to call the chooseJSFunction().

 
Originally posted by: GilletteCat
function chooseJSFunction() {
// Random number 0 - 2 inclusive
var randNum = Math.floor(Math.random() * 3)
switch (randNum) {
case 0:
FunctionOne();
break;
case 1:
FunctionTwo();
break;
default:
FunctionThree();
}
}
Where FunctionOne, FunctionTwo and FunctionThree are the exact names of the functions included inside the source js file you linked above. Afterwards, you can use the body.onload, as was suggested, to call the chooseJSFunction().

Truthfully, your solution is the way I would have done it. The reasoning for the way I did it in my post was because I saw that he wanted to include totally separate JS files in the OP for whatever reason.

Originally posted by: GilletteCat
And the other reason is lack of actual function calls (that is actually what you wanted, calling functions randomly). Just by loading source files containing some functions, the execution of those will not magically begin.

That is partially not true, the functions could all be "overloaded" with the same name.

Either way, a solution was reached. Just another notch in the belt for the AT Programming forum. :beer:
 
Originally posted by: tfinch2
Originally posted by: GilletteCat
function chooseJSFunction() {
// Random number 0 - 2 inclusive
var randNum = Math.floor(Math.random() * 3)
switch (randNum) {
case 0:
FunctionOne();
break;
case 1:
FunctionTwo();
break;
default:
FunctionThree();
}
}
Where FunctionOne, FunctionTwo and FunctionThree are the exact names of the functions included inside the source js file you linked above. Afterwards, you can use the body.onload, as was suggested, to call the chooseJSFunction().

Truthfully, your solution is the way I would have done it. The reasoning for the way I did it in my post was because I saw that he wanted to include totally separate JS files in the OP for whatever reason.

Originally posted by: GilletteCat
And the other reason is lack of actual function calls (that is actually what you wanted, calling functions randomly). Just by loading source files containing some functions, the execution of those will not magically begin.

That is partially not true, the functions could all be "overloaded" with the same name.

Either way, a solution was reached. Just another notch in the belt for the AT Programming forum. :beer:

The .js's actually exist on other sites, and have ID's attached to them--so what I did was link to each of them.
 
Back
Top