#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    67
    Rep Power
    14

    using javascript to link to external javascript files


    Can you use javascript itself to link a file to an external javascript file? I mean instead of using <script src="script.js"></script>....

    I want to be able to link to a different .js file on load depending on a variable passed to a page via the querystring.


    Please help!
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    You could simply use a variable based select condition. Depending upon the option a different document.write statement is encountered. This would be in the form....

    case "b":
    document.write("<script type=\"text/javascript src=\"whatever.js\"></script>")
    break;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    67
    Rep Power
    14
    I tried that, but you can't put a script tag within a script tag..... (which you have to do if your gonna use document.write!!)
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    I've used this effectively with IE (not tested on anything else though):

    <script language="JavaScript">
    <!---
    // change number in following line of code to match the menu item appropriate to this page (eg: menu item 1 = 1 )
    var currLoc = 0

    if (currLoc == 0) {
    document.write ("<script language='JavaScript' src='./JavaScript/mainMenu-index.js'></script>")
    } else {
    document.write ("<script language='JavaScript' src='./JavaScript/mainMenu-2.js'></script>")
    }
    //--->
    </script>

    So it does work!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    67
    Rep Power
    14
    Nope, I've found that it can freeze up Netscape 4.x ...... (which I have to code for)

    But, I've come up with a wierd solution, if you split the tags up in the string like so:-

    document.write("<scr"+"ipt src=\""+scriptName+".js\"><\/scr"+"ipt>");

    It works a treat!!!

    Go figure!! Netscape is still rubbish......
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    You don't need to tell me about Netscape being rubbish. Behold the joys of Netscape 6, where things work one second then a refresh stops things, and another refresh makes things work again. The same code but with two different targets don't produce the same results (when there's no reason why not). Leave the PC for five minutes and the DHTML is no longer animated, and the big crunch being that things that work in the Netscape release notes just don't in the release.
  12. #7
  13. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    All you need is this:

    document.write('<script src="' + scriptName + '.js"></' , 'script>');

    to keep the closing script tag from being interpreted as an "end of script block" tag.

    document.write() accepts multiple arguments and, as you can see , there's a good reason to use single quotes to delimit JS strings (it's HTML).
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    67
    Rep Power
    14
    Or you can use the '\' character to escape full quotes or any other character that may confuse poor netscape!

IMN logo majestic logo threadwatch logo seochat tools logo