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

javascript - Waiting on JS class load from dynamic script loading - Stack Overflow

programmeradmin2浏览0评论

I have a JS script which depends on jQuery.

I want to check for jQuery, and if it is not loaded/available add it myself, wait for it to load, and then define my script class.

The code I currently use:

// load jQuery if not loaded yet
if (typeof (jQuery) == 'undefined') {
  var fileref = document.createElement('script');
  fileref.setAttribute("type", "text/javascript");
  fileref.setAttribute("src", '.4.4.min.js');
  document.getElementsByTagName('body')[0].appendChild(fileref);

  (ready = function() {
    if ( typeof (jQuery) == 'undefined' || !jQuery) {
      return setTimeout( ready, 1 );
    } else {
      // jQuery loaded and ready
      jQuery.noConflict();
    }
  })();
}

// … class definition follows
var MView = function() …

Now, with FireFox 4 (I think it did work before, or execution was just too slow), it will continue the scripts execution even when I still want to wait on jQuery. The recursive setTimeout is non-blocking.

How can I fix this? Make setTimeout blocking? Use another approach? Is there a better way? A way at all?

The class should be global scope, so it can be used on the page that includes this script file.

I have a JS script which depends on jQuery.

I want to check for jQuery, and if it is not loaded/available add it myself, wait for it to load, and then define my script class.

The code I currently use:

// load jQuery if not loaded yet
if (typeof (jQuery) == 'undefined') {
  var fileref = document.createElement('script');
  fileref.setAttribute("type", "text/javascript");
  fileref.setAttribute("src", 'http://code.jquery./jquery-1.4.4.min.js');
  document.getElementsByTagName('body')[0].appendChild(fileref);

  (ready = function() {
    if ( typeof (jQuery) == 'undefined' || !jQuery) {
      return setTimeout( ready, 1 );
    } else {
      // jQuery loaded and ready
      jQuery.noConflict();
    }
  })();
}

// … class definition follows
var MView = function() …

Now, with FireFox 4 (I think it did work before, or execution was just too slow), it will continue the scripts execution even when I still want to wait on jQuery. The recursive setTimeout is non-blocking.

How can I fix this? Make setTimeout blocking? Use another approach? Is there a better way? A way at all?

The class should be global scope, so it can be used on the page that includes this script file.

Share Improve this question asked Mar 23, 2011 at 22:36 KissakiKissaki 9,2675 gold badges42 silver badges45 bronze badges 2
  • Possible duplicate of dynamic script loading synchronization – jherax Commented Aug 9, 2016 at 17:07
  • Check this related answer out: Load ordering of dynamically added script tags – jherax Commented Aug 9, 2016 at 17:07
Add a ment  | 

1 Answer 1

Reset to default 7

I would remend 2 things.

  1. Use 'if (!jQuery)' since undefined is considered falsey
  2. Use the script tag's onload event

if (!window.jQuery) {
    var fileref = document.createElement('script');
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", 'http://code.jquery./jquery-1.4.4.min.js');
    fileref.onload = function() {
        // Callback code here
    };
    document.getElementsByTagName('body')[0].appendChild(fileref);
}
发布评论

评论列表(0)

  1. 暂无评论