script.readyState is undefined in IE11 & FF

会有一股神秘感。 提交于 2019-12-07 02:13:27

From the MSDN:

Note: For the script element, readyState is no longer supported. Starting with Internet Explorer 11, use onload. For info, see Compatibility changes.

So, instead of checking for the readyState you can use something like this:

if (!script.addEventListener) {
    //Old IE
    script.attachEvent("onload", function(){
        // script has loaded in IE 7 and 8 as well.
        callBack();
    });
}
else
{
    script.addEventListener("load", function() {
        // Script has loaded.
        callBack();
    });
}

Also, I'm pretty sure that you can improve your code. And setInterval() is more suitable in this case. Read a little bit about how to use dom events and if you still have compatibility issues, here is something that you could use:

function loadExtScript(src, test, callback) {
  var s = document.createElement('script');
  s.src = src;
  document.body.appendChild(s);

  var callbackTimer = setInterval(function() {
    var call = false;
    try {
      call = test.call();
    } catch (e) {}

    if (call) {
      clearInterval(callbackTimer);
      callback.call();
    }
  }, 100);
}

The function takes a test as a parameter. Since you are the designer of the app, you’ll know what successful test is. Once this test is true, it will execute the callback.

Actually I think you're asking if the document is ready before it becomes ready. You need to set up a state change listener and check each time the document state changes.

document.onreadystatechange = function () {
  if (document.readyState == "complete") {
    initApplication();
  }
}

Read the documentation: https://developer.mozilla.org/en-US/docs/Web/API/document.readyState

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!