最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to load Jquery.js if not already loaded ? - Stack Overflow

programmeradmin0浏览0评论

I need to check if I need to load JQuery OR it is already loaded by another page !?

How can I check this on client side ? I want to have something like this:

<script >
 if jquery-1.6.2.js isExist then
   {return}
 else // somehow 
   load (<script src="../js/jq/jquery-1.6.2.js"></script> )
</script>

I need to check if I need to load JQuery OR it is already loaded by another page !?

How can I check this on client side ? I want to have something like this:

<script >
 if jquery-1.6.2.js isExist then
   {return}
 else // somehow 
   load (<script src="../js/jq/jquery-1.6.2.js"></script> )
</script>
Share Improve this question asked Dec 15, 2011 at 23:53 EmaxEmax 1,0473 gold badges13 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

This example from modernizr trays to load jquery from the google cdn if it fails it loads local jquery. Your code may be similar.

 <script src="//ajax.googleapis./ajax/libs/jquery/1.6.1/jquery.js"></script>
    <script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.1.min.js">\x3C/script>')</script>
<div id="loader"></div>
<script>
if (!window.jQuery) {
    var e = document.createElement('script'); e.async = true;
    e.src = '../js/jq/jquery-1.6.2.js';
    document.getElementById('loader').appendChild(e);
}
</script>
发布评论

评论列表(0)

  1. 暂无评论