IE9 没有运行 javascript onload
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10961430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IE9 not running javascript onload
提问by Charles John Thompson III
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.
出于某种原因,当浏览器第一次启动该会话时,IE9 没有在加载时运行我的 JavaScript 代码。似乎只有在用户刷新页面后才运行 onload。当调试控制台打开时,它也将运行 JavaScript。
How do I make it so the JavaScript runs onload after the browser is open? Is this just a bug of IE9?
如何在浏览器打开后让 JavaScript 在加载时运行?这只是IE9的错误吗?
I'll restate this so you understand: The code DOESN'Trun if you go to the site after launching a new browser session. The code DOESrun if you open the site in a new tab, or reload the page, or open the debug console
我将重申这一点,以便您理解:如果您在启动新的浏览器会话后访问该站点,则代码不会运行。代码DOES,如果你在新标签中打开该网站,或重新载入页面,或打开调试控制台运行
Here is the function I use to run my script onload (which works fine in NORMALbrowsers):
下面是我用它来运行我的脚本的onload(在正常工作的功能师范大学浏览器):
(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
采纳答案by Charles John Thompson III
Okay, I figured it out. It has to do with some weird way IE handles IF statements.
好吧,我想通了。它与 IE 处理 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.
在我的 init 函数中,我有两个 IF 语句,一个检查变量是否存在,然后记录该变量的值。另一个检查同一变量的值是否等于任意字符串。
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 语句后,似乎一切正常。我还决定使用不同的 onload 函数,如下所示:
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');
}
}
}
回答by Ryan
I had the exact same issue that you had. I had a set of images that I wanted to ensure were preloaded before I began starting a slideshow. I was making use of
我遇到了与您完全相同的问题。我想确保在开始幻灯片放映之前预加载了一组图像。我正在利用
$(window).load(function(){
//All my code
});
And this is exactly what I was facing.
而这正是我所面临的。
- When I copied and pasted the URL in IE, the onload event did not seem to fire.
- If I open the console using F12 and then past the URL in the browser and pressed enter, the everything seemed to be working.
- Now that I opened the console at least once,
- If I closeed the console and then reloaded the page, the onload was firing.
- If I typed the URL and then pressed enter, the onload was firing.
- 当我在 IE 中复制并粘贴 URL 时,onload 事件似乎没有触发。
- 如果我使用 F12 打开控制台,然后通过浏览器中的 URL 并按 Enter,一切似乎都在工作。
- 现在我至少打开了一次控制台,
- 如果我关闭控制台然后重新加载页面,则 onload 正在触发。
- 如果我输入 URL 然后按回车键,onload 就会被触发。
It took me a couple of days to actually figure out what I was doing wrong.
我花了几天时间才真正弄清楚我做错了什么。
The issue was with the console.log statements. At a lot of places in my code, I had done a lot of console logging. Even one of the plugins that I was using - jplayer has a an uncommented console message somewhere in the code.
问题出在 console.log 语句上。在我的代码中的很多地方,我做了很多控制台日志记录。即使是我使用的插件之一 - jplayer 在代码中的某处也有一条未注释的控制台消息。
The issue was that, unless you open the console at least once in IE, the console object is not available. Which means that the code will fail at the first console.log that it encounters.
问题是,除非您在 IE 中至少打开一次控制台,否则控制台对象不可用。这意味着代码将在它遇到的第一个 console.log 处失败。
Now, I was in no mood to comment out all my console.log statements just for the sake of testing it in IE. So, this is what I did instead. Right at the top of my document.ready jquery function, I wrote this small snippet of code.
现在,我没有心情为了在 IE 中测试它而注释掉我所有的 console.log 语句。所以,这就是我所做的。就在我的 document.ready jquery 函数的顶部,我写了一小段代码。
if(!window.console){
console={};
console.log = function(){};
}
What it basically does is creates a dummy console.log function placeholder so that the code can run in IE but it will work only as long as console.log is the only console function that you are making use of in your code or in your plugins.
它的主要作用是创建一个虚拟的 console.log 函数占位符,以便代码可以在 IE 中运行,但只有当 console.log 是您在代码或插件中使用的唯一控制台函数时,它才能工作.
Just my 2 cents. Been pulling my hair over this issue for longer than I care to admit. I hope this is useful to at least someone.
只有我的 2 美分。我在这个问题上纠结的时间比我承认的要长。我希望这至少对某人有用。
回答by Nickoli Roussakov
You need to figure out if the code doesn't run at all, I.e. never enters your function, or if it fails on some specific line inside your function. Does IE9 show any warnings or js errors?
您需要弄清楚代码是否根本不运行,即从不进入您的函数,或者它是否在函数内的某些特定行上失败。IE9 是否显示任何警告或 js 错误?
The easiest thing to do is stick a bunch of alert() statements in the code to see where it stops and narrow down to that line.
最简单的方法是在代码中粘贴一堆 alert() 语句,以查看它在哪里停止并缩小到该行。
If it never enters your function then you need to look higher, where the call is being made.
如果它永远不会进入您的函数,那么您需要查看更高的位置,即正在调用的位置。
回答by Ahmed Hamdy
Just a small note; When you use any debugging keywords (like console.log) or anything related, IE9 will escape this JS function if and only if the debugger is not on (with F12)
只是一个小笔记;当您使用任何调试关键字(如 console.log)或任何相关内容时,当且仅当调试器未打开(使用 F12)时,IE9 才会转义此 JS 函数
Actually I don't know what else cause a problem, but for me, my problem was the word "console.log" while debugger not on in IE9 ... I know this is already an answered question, but I felt it needs to be be known.
其实我不知道还有什么问题会导致问题,但对我来说,我的问题是“console.log”这个词,而调试器在 IE9 中没有打开......我知道这已经是一个已回答的问题,但我觉得它需要被人知道。