Emax
Emax

Reputation: 1047

How to load Jquery.js if not already loaded ?

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>

Upvotes: 2

Views: 3742

Answers (2)

neworld
neworld

Reputation: 7793

<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>

Upvotes: 5

szym
szym

Reputation: 3198

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.com/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>

Upvotes: 6

Related Questions