IE JQuery准备功能不起作用 [英] IE JQuery ready function not working
问题描述
显然很多人都遇到了这个问题,但是我还没有找到一个可行的解决方案.
Apparently many people have run into this problem, but I have yet to be able to find a solution that works.
我有一些代码需要在页面加载后运行,因此我将其粘贴在以下代码块中:
I have a bit of code which needs to run once the page has loaded, and so I stuck it inside the following block:
$(document).ready(function() {
alert("Running initialization");
initialize();
});
function checkDivLoaded() {
if ( $('#footer').length == 0) $.error( 'not ready' );
}
function initialize() {
try{
checkDivLoaded();
...more code here
} catch (err) {
setTimeout(initialize, 200);
}
}
除IE之外,这在所有浏览器中均能正常工作.在那里,代码根本不执行.
This works fine in all browsers, with the exception of IE. There, the code does not execute at all.
此代码位于我可以放置在页面上的最低点(使用Zend Framework和特定于页面的ready()函数意味着它可以走到多低的限制).我已经检查了js文件的包含文件,这些文件都是从本地版本加载的,并且都具有以下形式
This code is at the lowest point on the page that I can put it (using the Zend Framework and page-specific ready() functions means that there is a limit to how low on the page it can go). I've checked the includes for the js files, which are all being loaded from a local version, and they all have the form
<script type="text/javascript" src=""></script>
有什么想法吗?
注意
当我在IE中打开调试器时,这将开始正常工作.
When I open the debugger in IE, this starts to work correctly.
推荐答案
我在这里没有发现任何问题,但是我想指出一些事情.
I don't see anything wrong here, but there are just a few things I'd like to point out.
在执行此操作之前,请检查IE的JavaScript控制台,我猜是较早的脚本有错误,这就是为什么这个脚本永远不会运行的原因.
Before I do that, check IE's JavaScript console, my guess is an earlier script has an error, and that's why this one never gets run.
实际上,您可以只执行$(function(){})
而不是$(document).ready(function(){});
.但是,这应该没有什么不同.
Instead of $(document).ready(function(){});
, you can actually just do $(function(){})
. it shouldn't make a difference, though.
另外,请不要将字符串传递给setTimeout
(它们在全局范围内得到eval
),请传递函数.
Also, please don't pass strings to setTimeout
(they get eval
'd in the global scope), pass a function.
setTimeout(initialize, 200);
还可以确定您声明了$jquery
(不是$.error
)吗?
Also, are you sure you declared $jquery
(shouldn't it be $.error
)?
<script type="text/javascript">
$(function() {
initialize();
});
function checkDivLoaded() {
if ($('#footer').length == 0){
$jquery.error( 'not ready' );
}
}
function initialize() {
try{
checkDivLoaded();
//...more code here
} catch (err) {
setTimeout(initialize, 200);
}
}
</script>
如果您使用的是IE8或更低版本,除非打开调试器,否则它不会添加console
(我希望有人认为这是个好主意已被解雇).如果您的include之一使用console.log
,它将是undefined
,从而停止所有脚本执行.
If you're using IE8 or lower, it doesn't add console
unless the debugger is open (I wish whoever thought that was a good idea has been fired). If one of your includes uses console.log
it would be undefined
, thus stopping all script execution.
尝试将其添加到页面顶部:
Try adding this to the top of the page:
window.console = window.console || {log:function(){}};
它将创建一个伪" console
对象,以便定义console.log
.
It will make a "fake" console
object, so that console.log
will be defined.
这篇关于IE JQuery准备功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!