Suppose we load jquery.min.js in header of a page and if my page contains a div whose contents are loaded from another page which also contains jquery.min.js so exactly how many times jquery is loaded in browser one time or two times?
-
put the jquery.mim.js in a single location and load it,rather than loading it in every page. – Techy Sep 01 '14 at 05:34
1 Answers
If the script elements in the content you load are executed (which may or may not be the case), jQuery will get loaded a second time. The question is: What happens when that happens?
The answer is: It depends, but it's probably not good.
If nothing has kept a reference to the old jQuery function, and you haven't added any plugins, and you haven't registered any event handlers or set any data using the data
function, things should be fine.
If you have done any of those things, it can get confusing fast, because if you've kept a reference to the previous copy of jQuery, perhaps like this:
jQuery(function($) {
// Using $ here...
});
...you'll have some code using the first copy and perhaps some other code using the second copy.
Plugins are attached to the current copy of jQuery, so loading it a second time means those plugins aren't available on the freshly-loaded copy.
When you set up event handlers or save data with the data
function, jQuery stores information within a data structure it holds internally, so those handlers and that data won't be available to the other copy. In terms of event handlers, that only really matters if you try to remove them with the newly-loaded copy.
Bottom line: Best to avoid it.

- 1,031,962
- 187
- 1,923
- 1,875