3

I want to know that when i add jquery in page i want to check before i add that does that jquery already added or not if not added then only add else not.

for example i have added jquery jquery-1.4.1.min

<script type="text/javascript" src="<?php echo SITE_JS;?>jquery-1.4.1.min"></script>

after that when i am trying to add jquery jquery-1.7.1.min

in case i didnt know that i have already added or not so i tried to add it but before that i want to check the version if the jquery with the old version or same then i dont want it to add else add new jquery and disable older one.

<script type="text/javascript" src="<?php echo SITE_JS;?>jquery-1.7.1.min"></script>

before adding this i want to check does jquery already exist with higher version or not. how is it possible?

Joe Wicentowski
  • 5,159
  • 16
  • 26
Yadav Chetan
  • 1,874
  • 2
  • 23
  • 43

3 Answers3

10

Try it like,

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
    window.jQuery || document.write('<script src="<?php echo SITE_JS;?>/jquery-1.9.1.min.js"><\/script>');
 // you should have jquery-1.9.1.min.js on the site url
</script>
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
3

try this method to check if jquery is loaded or not...if loaded print the version...

if (typeof jQuery != 'undefined') {  
        // jQuery is loaded => print the version
          alert(jQuery.fn.jquery);}

based on the version write a logic to load your version of jquery...

John Smith
  • 1,095
  • 2
  • 11
  • 24
3

use "window.load" event

(function() {
if (window.addEventListener) {
    // Standard
    window.addEventListener('load', jQueryCheck, false);
}
else if (window.attachEvent) {
    // Microsoft
    window.attachEvent('onload', jQueryCheck);
}
function jQueryCheck() {
    if (typeof jQuery === "undefined") {
        // No one's loaded it; either load it or do without
    }
}
})();

Ref

Community
  • 1
  • 1
Mayank Awasthi
  • 337
  • 3
  • 14