Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jan 2003
    Location
    West Virginia
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    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);

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.

  • #4
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    @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.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Jan 2003
    Location
    West Virginia
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    @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.

  • #11
    Regular Coder
    Join Date
    Jan 2003
    Location
    West Virginia
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #12
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    Seems to work fine here: http://eg.jfein.net/help/renoCF/

  • #13
    New Coder
    Join Date
    Mar 2012
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can u use php or u want client side?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •