Reputation: 85056
I am in a situation where I have multiple ASCX files that are being added to an aspx page. Some of these files include the jQuery library. Depending on which ones are added the jQuery library may be included more than once.
I cannot add jQuery at the masterpage or some other level, it must be from the ASCX. The reasons for this are beyond the scope of this question, but if someone really wants an explanation I can provide one.
Is it bad to have jQuery added multiple times in an ASPX page? Is there a way to conditionally add a script to an ASCX page?
Upvotes: 3
Views: 3026
Reputation: 219938
You should not add jQuery more than once.
You can add it conditionally though:
<script>window.jQuery || document.write('<script src="js/jquery-1.8.2.min.js">\x3C/script>')</script>
This is how they to it in the html5boilerplate
Upvotes: 7
Reputation: 31033
in your ascx
s you can do
if (typeof jQuery == 'undefined') {
// load jquery
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "path/to/jQuery";
document.getElementsByTagName('head')[0].appendChild(script);
} else {
//already loaded
}
ref: check if jquery has been loaded, then load it if false
Upvotes: 8