If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Can a js randomly load another js

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

Code:

<SCRIPT language="JavaScript" SRC="ss-1.js"></SCRIPT>

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....

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...

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.

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.

I saved your script exactly as written as:

Code:

<script src='random_ss.js' type='text/javascript></SCRIPT>

I placed it in the same folder with the other slideshow javascripts, which were named: ss-1.js, ss-2.js, ss-3.js...

(I know for a fact that each of these ss-x.js will work when referenced directly, as I used them repeatedly).

The photos were referenced in each ss-x.js to a separate folder, as in ../slides/slide1.jpg

With all that, I got no error message, no slides, nothing but an emply space on the html page.

Anyway, sorry to have offended you ~ was not my intention ~ if the mods want to delete the thread before anyone else gets upset, please do so.