Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you use a <script> tag with the same "src" attribute multiple times within a single HTML document?

Tags:

Although I am almost certain the answer to this question will be browser specific, do any of the browsers define behavior for when multiple <script> tags are used and have the same src attribute?

For instance...

<script src="../js/foo.js"></script> ...  <!-- what happens here? --> <script src="../js/foo.js"></script> 

The reason I ask this question in the first place, is that in my particular case I am using partial views in an ASP.NET MVC application which make use of JQuery. The JQuery JS file(s) are all included in the master template file via script tags. I would prefer to add script tags to the partial view files so that in case they are used outside the context of the master template, they will automatically include all the necessary JS files, and not rely on another view or template to include them. However, I certainly don't want to cause JS files to have to be transferred to the client multiple times, or any other side effects that could negatively impact the user experience.

My thinking right now is that most, if not all, of the major browsers (FF, Safari, IE, Opera) will cache a JS file the first time it is used, and then on subsequent script tags the browser will use the cached copy if available and if it hasn't expired. However, caching behavior can usually be altered through browser configuration, so it doesn't seem too "safe" to rely on any kind of caching behavior.

Will I just have to accept the fact that my partial views are going to have be dependent on other templates or views including the appropriate JS files?

like image 677
Justin Holzer Avatar asked Oct 07 '09 19:10

Justin Holzer


People also ask

Can you use multiple script tags in HTML?

An HTML page can contain multiple <script> tags in the <head> or <body> tag. The browser executes all the script tags, starting from the first script tag from the beginning.

Can I use the same script twice?

It is possible to run the same script twice; however, it may require significant rewriting to allow that to work. The problem happens when there is a specific thing (maybe a property, maybe variable, maybe a command) that is unique in the script.

How many script tags can you have in HTML?

Multiple <SCRIPT> Tags Up to this point all of the JavaScript Code was in one <SCRIPT> tag, this does not need to be the case. You can have as many <SCRIPT></SCRIPT> tags as you would like in a document.

What does script src do in HTML?

Definition and Usage The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again.


1 Answers

Even if they're cached, you may have problems since the same code will be executed twice. At the very least, this will cause the browser to take more time than necessary. And it may cause errors, since most JavaScript code isn't written to be executed twice. For example, it may attach the same event handlers twice.

like image 111
JW. Avatar answered Oct 16 '22 08:10

JW.