Attach jQuery if it doesn’t exist or if an older version exists

function version_compare(a,b){var c=a.split('.');var d=b.split('.');for(var i=0;i<c.length;++i){if(d.length==i){return"gt"}if(c[i]==d[i]){continue}else if(c[i]>d[i]){return"gt"}else{return"lt"}}if(c.length!=d.length){return"lt"}return"eq"}

var requiredJQueryVersion = '1.7';

if(!window.jQuery || version_compare(window.jQuery.fn.jquery,requiredJQueryVersion)=='lt') {
    var s = document.createElement('script');
    // Grab jQuery from Google's CDN
    s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/'+ requiredJQueryVersion+'/jquery.min.js');
    s.setAttribute('type', 'text/javascript');
    document.getElementsByTagName('head')[0].appendChild(s);
}

Leave a Reply