changing scripts on the fly - works in IE but not in FF

Discussion in 'Javascript' started by yawnmoth, Jul 8, 2006.

  1. yawnmoth

    yawnmoth Guest

    <script>
    document.getElementsByTagName(/script/.source)[0].src='http://www.domain.tld/script.js';
    </script>

    If script.js does simply "alert('hello, world!')" then only IE will
    actually display the "hello, world!" message. FireFox will do nothing.
    Since, in this case, I prefer IE's behavior, does anyone have any
    ideas as to what I could do to get this working in FireFox?
     
    yawnmoth, Jul 8, 2006
    #1
    1. Advertisements

  2. yawnmoth

    Randy Webb Guest

    yawnmoth said the following on 7/8/2006 1:23 AM:
    Thats because IE is the only one that allows you to change the .src
    property that way.
    Probably puts an error message in the Error Console.

    Why not a way that works in any dynamic browser?

    function loadJSFile(fileURL){
    var newScript = document.createElement('script');
    newScript.type = "text/javascript";
    newScript.src = fileURL;
    document.getElementsByTagName('head')[0].appendChild(newScript);
    }

    and then call it like this:

    loadJSFile('URLToJSFile');
     
    Randy Webb, Jul 8, 2006
    #2
    1. Advertisements

  3. Just as a comment (since Randy Webb already answered your question),
    using /script/.source is just a convoluted way of writing the string
    "script".

    /L
     
    Lasse Reichstein Nielsen, Jul 8, 2006
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.