Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-11-2012, 05:40 PM   PM User | #1
Reno CF
Regular Coder

 
Join Date: Jan 2003
Location: West Virginia
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
Reno CF is an unknown quantity at this point
Question 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....
__________________
Reno CF
Reno CF is offline   Reply With Quote
Old 11-11-2012, 06:09 PM   PM User | #2
Nile
Regular Coder

 
Nile's Avatar
 
Join Date: Jun 2008
Posts: 280
Thanks: 2
Thanked 46 Times in 46 Posts
Nile is an unknown quantity at this point
You could use Javascript to make a new <script> element, though I'm not sure if it's considered bad practice or not.

Code:
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);
Nile is offline   Reply With Quote
Old 11-11-2012, 06:16 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Try this:-

Code:
<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.
Philip M is offline   Reply With Quote
Old 11-11-2012, 06:25 PM   PM User | #4
Nile
Regular Coder

 
Nile's Avatar
 
Join Date: Jun 2008
Posts: 280
Thanks: 2
Thanked 46 Times in 46 Posts
Nile is an unknown quantity at this point
@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.
Nile is offline   Reply With Quote
Old 11-11-2012, 06:37 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-11-2012, 06:41 PM   PM User | #6
Nile
Regular Coder

 
Nile's Avatar
 
Join Date: Jun 2008
Posts: 280
Thanks: 2
Thanked 46 Times in 46 Posts
Nile is an unknown quantity at this point
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.
Nile is offline   Reply With Quote
Old 11-11-2012, 08:37 PM   PM User | #7
Reno CF
Regular Coder

 
Join Date: Jan 2003
Location: West Virginia
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
Reno CF is an unknown quantity at this point
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]);
__________________
Reno CF
Reno CF is offline   Reply With Quote
Old 11-11-2012, 08:48 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by felgall View Post
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.
Philip M is offline   Reply With Quote
Old 11-11-2012, 09:00 PM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Reno CF View Post
document.write(ss[increment]);
That statement is also long obsolete. Netscape 4 was the last browser where document.write served any purpose.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-11-2012, 09:06 PM   PM User | #10
Nile
Regular Coder

 
Nile's Avatar
 
Join Date: Jun 2008
Posts: 280
Thanks: 2
Thanked 46 Times in 46 Posts
Nile is an unknown quantity at this point
@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.
Nile is offline   Reply With Quote
Old 11-11-2012, 09:38 PM   PM User | #11
Reno CF
Regular Coder

 
Join Date: Jan 2003
Location: West Virginia
Posts: 110
Thanks: 0
Thanked 0 Times in 0 Posts
Reno CF is an unknown quantity at this point
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.
__________________
Reno CF
Reno CF is offline   Reply With Quote
Old 11-11-2012, 09:48 PM   PM User | #12
Nile
Regular Coder

 
Nile's Avatar
 
Join Date: Jun 2008
Posts: 280
Thanks: 2
Thanked 46 Times in 46 Posts
Nile is an unknown quantity at this point
Seems to work fine here: http://eg.jfein.net/help/renoCF/
Nile is offline   Reply With Quote
Old 11-11-2012, 11:02 PM   PM User | #13
elhippie
New Coder

 
Join Date: Mar 2012
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
elhippie is an unknown quantity at this point
can u use php or u want client side?
elhippie is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:12 AM.


Advertisement
Log in to turn off these ads.