简体   繁体   中英

Run after jQuery version selection is done

I'm trying to find out if a user supports jQuery 2.x
This works fine, but when I try to run a script it doesn't work because jQuery isn't done loading...

How can I trigger __run() after jQuery is done loading.

Init script:

function __run(){
    //
    // function runs the jQuery website
    //  
    $("body").append( "<p>Test</p>" );
}

(function () {
    var s, s0, js;
    if (typeof JSON !== 'undefined' && 'querySelector' in document && 'addEventListener' in window) {
        js = '//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js';
    } else {
        js = '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js';
        alert('Woow, that\'s one old browser, maybe you should upgrade. We don\'t support this version, you can try to use the website though');
    }
    s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = js;
    s0 = document.getElementsByTagName('script')[0];
    s0.parentNode.insertBefore(s, s0);
    __run();
}());

Check with onreadystatechange

s.onload = s.onreadystatechange = function(){
    if ( !done && (!this.readyState ||
    this.readyState == "loaded" || this.readyState == "complete") ) {

         __run();
    }
};

Try this

(function () {
    var s, s0, js;
    if (typeof JSON !== 'undefined' && 'querySelector' in document && 'addEventListener' in window) {
        js = '//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js';
    } else {
        js = '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js';
        alert('Woow, that\'s one old browser, maybe you should upgrade. We don\'t support this version, you can try to use the website though');
    }
    s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = js;
    s0 = document.getElementsByTagName('script')[0];
    s0.parentNode.insertBefore(s, s0);

    s.onreadystatechange= function () {
      if (this.readyState == 'complete') __run();
    }
    s.onload= __run;
}());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM