IE9 js加载顺序和JQuery

时间:2011-11-02 23:04:18

标签: jquery internet-explorer internet-explorer-9

这似乎是一个容易解决的问题,但这是我的屁股。

以下简单示例在IE9中产生错误。我已经使用IE9的开发者页面来监控网络加载,看起来IE正在加载并执行主体的脚本,然后才加载/评估包含的javascript。

我尝试了通过搜索答案找到的所有各种技巧,包括添加<meta charset="UTF-8" /><meta http-equiv="x-ua-compatible" content="IE8"/>

<html>
<head>
  <title>Demo of IE suckiness</title>
  <script type='text/javascript' 
      src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
  </head>
  <body>
This works in every browser but not IE9. IE gives the error:
<pre>
Line: 9
Error: The value of the property '$' is null or undefined, not a Function object
</pre>

<script type="text/javascript" defer="defer">
$(document).ready(function(){
  alert("Must not be IE");
});
</script>
</body>
</html>

我有一个解决方法。将准备好的脚本移动到<body onload="ready()">,但我不能相信IE9已经坏了,我一定做错了什么而且看不到它。

4 个答案:

答案 0 :(得分:3)

据我所知,当整个文档(包括所有图像和相关内容)都已加载时,IE9只会触发$ .load()。可怕但真实!我的解决方法是:

            var docReady = {
                actions:new Array(),
                flag:0,
                time:0,
                period:10,
                trigger:function() {this.flag=1},
                checkHandle:0,
                check:function() {
                    this.time+=this.period;
                    if(this.flag) {
                        clearInterval(this.checkHandle);
                        for(i in this.actions) {this.actions[i]();}
                    }
                },
                watch:function() {
                    this.checkHandle = setInterval(function() {docReady.check();},this.period);
                },
                action:function(f) {
                    this.actions.push(f);
                }

            };

            docReady.watch();
确实是一个肮脏的,但它正在发挥作用;现在,只要您认为合适,就可以调用docReady.trigger(),至少应该在页面末尾执行。此外,您可以将其称为body onload。保证你使用docReady.action()添加的函数只运行一次:第一次触发()时发生。

答案 1 :(得分:2)

我有类似的问题。我用resetting all my IE9 settings修正了它 我认为通过启动安全性或隐私,它可以破坏外部页面的jquery加载。

答案 2 :(得分:0)

您是否试图遗漏defer="defer"

答案 3 :(得分:0)

我遇到了同样的问题并且通过不使用Google托管的jQuery并在本地托管它来解决它。

相关问题