开发者

Is it bad to add jQuery multiple times on a page?

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?


in your ascxs 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜