...

View Full Version : Can a js randomly load another js



Reno CF
11-11-2012, 05:40 PM
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
<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....

Nile
11-11-2012, 06:09 PM
You could use Javascript to make a new <script> element, though I'm not sure if it's considered bad practice or not.



var script = document.createElement("script");
script.src = "ss-" + (Math.floor(Math.random()*5)+1) + ".js";
script.type = "text/javascript"; //language attribute is deprecated

document.head.appendChild(script);

Philip M
11-11-2012, 06:16 PM
Try this:-


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

Nile
11-11-2012, 06:25 PM
@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.

felgall
11-11-2012, 06:37 PM
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.

Nile
11-11-2012, 06:41 PM
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.

Reno CF
11-11-2012, 08:37 PM
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...




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]);

Philip M
11-11-2012, 08:48 PM
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.

felgall
11-11-2012, 09:00 PM
document.write(ss[increment]);


That statement is also long obsolete. Netscape 4 was the last browser where document.write served any purpose.

Nile
11-11-2012, 09:06 PM
@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.

Reno CF
11-11-2012, 09:38 PM
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:

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

Nile
11-11-2012, 09:48 PM
Seems to work fine here: http://eg.jfein.net/help/renoCF/

elhippie
11-11-2012, 11:02 PM
can u use php or u want client side?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum