Sunday, June 2, 2024
97
rated 0 times [  102] [ 5]  / answers: 1 / hits: 18463  / 14 Years ago, thu, february 3, 2011, 12:00:00

One of the webapps I'm working in is made up of many partial HTML files. If the partial requires a JavaScript library such as YUI, the YUI library is included in the partial.



When the partials are combined at runtime, the resulting HTML often includes the YUI library several times.



<html>
...
<script type=text/javascript src=/js/yahoo/yahoo-min.js></script>
...
<script type=text/javascript src=/js/yahoo/yahoo-min.js></script>
...
<script type=text/javascript src=/js/yahoo/yahoo-min.js></script>
...
</html>


I've seen strange behavior from including jQuery several times, especially when using AJAX. Why, specifically, is including the same JavaScript library more than once a bad idea? Why does it only sometimes cause problems?


More From » javascript-framework

 Answers
19

Depending on the library, including it more than once could have undesired effects.



Think of it like this, if you have a script that binds a click event to a button, and you include that script twice, those actions will be ran twice when the button is clicked.



You could write a simple function that you call to load a script and have it keep track of files that have already been loaded. Or, I'm sure you could probably use a pre-existing JS loader such as LabJS and modify it.


[#93912] Wednesday, February 2, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
peytont

Total Points: 215
Total Questions: 110
Total Answers: 111

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
peytont questions
Thu, Oct 28, 21, 00:00, 3 Years ago
Mon, May 10, 21, 00:00, 3 Years ago
Sun, Nov 15, 20, 00:00, 4 Years ago
Thu, Jan 23, 20, 00:00, 4 Years ago
;