Can a javascript be loaded randomly from a list contained within another js? For example, let's say I have 5 js slideshows: ss-1.js, ss-2.js, ss-3.js, ss-4.js and ss-5.js.
On my html page, I could reference any one of them with the standard
but if I wanted to change the slideshow periodically, I would need to manually make the -x.js change, then upload again. It would require far less input to only use one js that simply loaded one of the ss-x.js files from an array, in the same manner that javascript is used to randomly display graphics, or randomly display a URL, or a banner, etc.
So I'm wondering if there is a way for a js ~ the one to be placed in position on the html page ~ to randomly load another js, when that other js is a slideshow script?
I hope that was explained clearly ~ thanks to all the js gurus for any suggestions....
<script type = "text/javascript">
// randomly generate name of js script
scriptNode = document.createElement('script');
scriptNode.src = nameofjsscript;
scriptNode.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(scriptNode);
</script>
Quizmaster: The Cairngorm Mountains are home to Britain's last free-ranging herds of which animals, popularly associated with Christmas?
Contestant: Turkeys.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
@Phillip M, Is there a difference between what you've posted and what I've posted? According to MDN, `document.head` returns the first `head` element on the page.
The only thing wrong with either of those is that JavaScript is supposed to go at the bottom of the body. Placing it in the head (with very few exceptions where you don't want the page to load) is obsolete.
With that being the current case, another alternative is to give the script tag you're working in an ID, get that script tag by the ID and use insertBefore to insert the randomly loaded Javascript before the script tag you're working in. This is all, of course, assuming that you're placing the original script tag right before the body closing tag.
Though, there still exists the option to just use document.body.appendChild(...) instead.
Thanks everyone ~ your feedback is appreciated. When I started playing with this idea this morning, I took an old random banner script that I had archived and tried to modify it to work with slideshow javascripts. Of course, it did not work ~ below is my feeble attempt. Where you see "ss", it originally had the word "banner"; where you see the "script src", it originally had the "img src".
What I like about this sort of format is how easily it is to add new slideshows to the array, but after much modifying and frustration, my lack of experience became apparent so thought I'd throw the question out here for consideration.
If in fact it is possible to write a js with a similar format as the one below, please give it a shot if you feel so inclined.... thanks again...
Code:
function ss() {
};
ss = new ss();
number = 0;
// ssArray
ss[number++] = "<script src='ss1.js' type='text/javascript'></script>"
ss[number++] = "<script src='ss2.js' type='text/javascript'></script>"
ss[number++] = "<script src='ss3.js' type='text/javascript'></script>"
// keep adding here...
increment = Math.floor(Math.random() * number);
document.write(ss[increment]);
The only thing wrong with either of those is that JavaScript is supposed to go at the bottom of the body. Placing it in the head (with very few exceptions where you don't want the page to load) is obsolete.
This is an example where placing the script in the <head> is perfectly proper. Although right ahead of the </body> tag is to be preferred, there is no real reason why scripts (especially small scripts such as this) cannot be placed in the <head>.
Nile - My solution and yours are effectively the same, but I did not see your post before I submitted mine. Both ought to work.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
@Reno CF, I see that you've incorporated neither Phillip M's nor my idea into your script and because of this outrageous display of ignorance, I'm wary to help you.
I come to a forum like this precisely because I suffer "outrageous ignorance" ~ if I knew what I was doing, I'd figure it out for myself. For the record, I did try your solution but it did not work. I made the assumption that the problem is with me, not with your script, so I did not mention it out of courtesy.