Results 1 to 3 of 3
  1. #1
    5 Star Lounger
    Join Date
    Apr 2003
    Location
    Hampshire, United Kingdom
    Posts
    602
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Referring to a .js file

    Hopefully this question will be embarrassingly easy for someone to answer...

    I have a number of javascripts in external (*.js) files. In one case, the script in one file wishes to call/run the script in another. The way I've done this is rather crude:

    document.write('<SCRIPT language=JavaScript src="targetscript.js" type=text/javascript></SCRIPT>');

    This works fine with Internet Explorer but is ignored by Netscape. Is there a cross-brower way of calling one external javascript file from another?
    Waggers
    If at first you do succeed, you&#39;ve probably missed something.

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referring to a .js file

    Just a though here (I haven't tested this)....

    You may try using Javascript formatting characters in your reference to the other script. IE is very forgiving for such things.

    Try replacing the quote characters with backslash+quote: document.write('<SCRIPT language="JavaScript" src="targetscript.js" type="text/javascript"></SCRIPT>');

    (I added a few extra quotes just to make the point; although you can probably leave them out, except for the ones surrounding your src file.)

    Hope this helps

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Referring to a .js file

    > Is there a cross-brower way of calling one external javascript file from another?

    I'm surprised IE allows it. My understanding is this: an external script file should contain only what is legal in your main document between a pair of <SCRIPT> and </SCRIPT> tags, no more, no less.

    I don't know the precise sequence in which the code in the files is processed versus when they are loaded. It is possible that all external files will be loaded before any of their code runs, or it could be that the code in each external file runs before the next is loaded. Either way, if you "import" the external files in the sequence needed for them to run properly under the "worst case scenario," that should be all you need. Hope this helps.

Posting Permissions

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