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 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sequence of javascript tag

    I just want to know if the following sequence of javascript code is possible.If possible what are the drawbacks and what are the considerations I should remember.
    Code:
    <script type="text/javascript" src="someFile.js"></script>
    
    <script type="text/javascript">
    // code goes here
    </script>
    
    <script type="text/javascript" src="anotherFile.js"></script>
    
    <script type="text/javascript" src="anotherFile.js"></script>
    
    <script type="text/javascript">
    // code goes here
    </script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Yes, of course that's possible.

    I don't know of any drawbacks, except that you might get an error because of the two files of the same name meaning that the second one will be redefining what the first one did. I'm kind of assuming that you meant for those ".js" files to all three be different names, not two the same.

    Considerations? Depends on if any of the code in any of those blocks does any "immediate" code or if all the code resides in functions.

    If all in functions, order won't matter at all.

    But if any of the code is *not* in functions, then what works and what doesn't and what works when will depend on the order.

    Example: If "anotherFile.js" defines the function
    Code:
    function framitz( ) { ... }
    then if your first inline <script> block contained a reference to that function that was not itself in a function you'd get a missing object (functions are objects) error.

    But I'm guessing you know all that.

    So I'm not clear what j=kind of "gotchas" you are worried about.

    Obviously if two or more of those blocks (either the .js files or the inline blocks) define the same thing, then the later one will "win". This can have consequences if two or more both try to, for example, attach an event to some object and don't do it such that the second one doesn't wipe out the first.

    But, really, all of this would be the same even if you didn't use ".js" files and just dumped all the code into a single <script> block, inline. Which is, effectively, what happens.


  •  

    Posting Permissions

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