View Full Version : sequence of javascript tag

06-19-2009, 11:36 PM
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.

<script type="text/javascript" src="someFile.js"></script>

<script type="text/javascript">
// code goes here

<script type="text/javascript" src="anotherFile.js"></script>

<script type="text/javascript" src="anotherFile.js"></script>

<script type="text/javascript">
// code goes here

Old Pedant
06-19-2009, 11:55 PM
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
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.