IE9没有运行javascript onload [英] IE9 not running javascript onload

查看:73
本文介绍了IE9没有运行javascript onload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,当第一次启动浏览器时,IE9没有运行我的JavaScript代码onload。它似乎只在用户刷新页面后运行onload。它还将在调试控制台打开时运行JavaScript。

For some reason, IE9 is not running my JavaScript code onload when the browser is launched for the first time that session. It seems to only run onload after the user refreshes the page. It will also run the JavaScript when the debug console is open.

如何使JavaScript在浏览器打开后运行onload?这只是IE9的一个错误吗?

How do I make it so the JavaScript runs onload after the browser is open? Is this just a bug of IE9?

我会重申这一点让你理解:如果你去的话,代码不会运行启动新的浏览器会话后的网站。如果您在新标签页中打开网站,或重新加载页面,或打开调试控制台,则代码 DOES 会运行

I'll restate this so you understand: The code DOESN'T run if you go to the site after launching a new browser session. The code DOES run if you open the site in a new tab, or reload the page, or open the debug console

这是功能我用来运行我的脚本onload(在 NORMAL 浏览器中工作正常):

Here is the function I use to run my script onload (which works fine in NORMAL browsers):

(function (i) {
  var u = navigator.userAgent;
  var e = /*@cc_on!@*/
  false;
  var st = setTimeout;
  if (/webkit/i.test(u)) {
    st(function () {
      var dr = document.readyState;
      if (dr == "loaded" || dr == "complete") {
        i()
      } else {
        st(arguments.callee, 10);
      }
    }, 10);
  } else if ((/mozilla/i.test(u) && !/(compati)/.test(u)) || (/opera/i.test(u))) {
    document.addEventListener("DOMContentLoaded", i, false);
  } else if (e) {
    (function () {
      var t = document.createElement('doc:rdy');
      try {
        t.doScroll('left');
        i();
        t = null;
      } catch (e) {
        st(arguments.callee, 0);
      }
    })();
  } else {
    window.onload = i;
  }
})(init); //init is the function to call onload


推荐答案

好的,我想到了。它与IE处理IF语句的一些奇怪方式有关。

Okay, I figured it out. It has to do with some weird way IE handles IF statements.

在我的init函数中,我有两个IF语句,一个检查变量是否存在,然后记录值那个变量。另一个检查相同变量的值是否等于任意字符串。

In my init function I had two IF statements, one which checked if a variable existed and then logged the value of that variable. The other which checked to see if the value of the same variable was equal to an arbitrary string.

删除第一个IF语句后,一切似乎都正常。我还决定使用不同的onload函数,如下所示:

After removing the first IF statement, everything seems to work properly. I also decided to use a different onload function which can be seen below:

if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', init, true);
} else if (document.all && !window.opera){ //Crude test for IE
//Define a "blank" external JavaScript tag
    document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
    var contentloadtag=document.getElementById("contentloadtag");
    contentloadtag.onreadystatechange=function(){
        if (this.readyState=="complete") {
            init();
            //ie('open');
        }
    }
}

这篇关于IE9没有运行javascript onload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆