2010-09-17 130 views
0

为什么我的Javascript小部件在IE上无法正常工作,但在Firefox上很好?而且,Firebug不会产生任何错误。我能做些什么来确保我的Javascript小部件在IE中运行?有什么工具可以帮助我吗?IE中的Javascript兼容性

+17

缩小的版本检查你没有粘贴的代码的第34行第5列。 – MooGoo 2010-09-17 02:28:02

+0

苛刻** MooGoo **,但它确实让我LOL。 – 2010-09-17 02:31:16

+0

这使我成为了LOL,但我想我正在寻找更多关于解决问题的一般答案。例如,IE浏览器的Firebug工具? – syker 2010-09-17 03:19:50

回答

2

似乎与IE的兼容性问题。您可以在右下角查看标准JavaScript错误警报图标。另外,IE Developer Toolbar很有用,但不如Firebug。最坏的情况下,开始投掷一些alerts,直到找到断点。

只是在黑暗中刺中,如果您使用的是console.log,那么在其他浏览器中会失败。作为一名开发人员,我之前已经离开过。

+0

我定义了一个自定义的全局函数,并用它来代替:function log(o){try {console.log(o); } catch(e){}} – letronje 2010-09-17 02:35:02

+0

出于某种奇怪的原因,我发现IE Dev Toolbar和IETTER内部使用它一样好萤火虫 – lock 2010-09-17 03:22:39

0

在IE8中打开小部件并使用它自带的跛脚(与Firebug相比)开发人员工具栏(键盘快捷键:F12)。

3

在IE中JS的一个常见问题是对象和数组文字中的尾随逗号:IE扼流圈和死亡。所有其他浏览器都很好。因此,寻找:

an_array = [1,2,3,]; // Trailing comma kills IE! 

an_obj = {foo: "This is foo", 
      bar: "This is bar", // Trailing comma kills IE! 
     }; 
+0

+1:我自己使用脚本在签入代码前使用regexp来检查它。我有时会得到误报,但这比我的老板在凌晨2点打电话给我更好地修复损坏的更新要好得多。 – slebetman 2010-09-17 03:35:47

+0

我不是IE的爱好者,但这是IE为改变做正确事情的典型例子。让坏代码破坏。 – 2011-06-22 07:32:52

2

的IE 6+都符合相当不错的ECMA规范(基本上涵盖了所有的核心,如日期,Math和Array对象的Javascript“programmey”对象 - 任何处理数学或数据类型)。然而,如果你正在处理任何涉及标准W3C DOM的东西(那些与访问HTML文档的任何部分或其中的事件有关的对象),那么很可能你的函数将在IE浏览器中出现kerplode,这一直落后于DOM规范超过十年。整个框架的建立是为了弥补这一点。如果您正在处理事件或访问HTML元素或其属性,您将希望使用像JQuery这样的框架,或者开始阅读一些关于JavaScript的书籍,以了解您需要分支的对象和属性。

要记住的另一件事是,所有的浏览器制造商通过实验的方式添加自己的专有方法。因此,Firefox的非标准但非常流行的console.log。为了公平对待MS(我仍然认为这是卑鄙的),他们的XMLHttpRequest对象的原始版本就是所有这些Ajax内容的代码,并且它们也给了我们innerHTML,它不是任何标准的一部分,但是被采用并且在所有浏览器。

基本上,所有浏览器都会解析和解释他们自己的JavaScript版本。学习所有常见的全面工作,以及如何处理所有他们无法达成的共识,取决于你。我推荐Jeremy Keith的DOM脚本,然后是大巨人O'Reilly的书(我也喜欢奥斯本的大巨人完整参考书)。

网站:Quirksmode.org的内容似乎比过去少,但在编写核心JS以弥补IE不足方面仍有很多好的建议。也有很多关于CSS的东西。

0

也许你需要从MDC

添加兼容算法这里是Array.everyArray.filterArray.forEachArray.indexOfArray.lastIndexOfArray.mapArray.reduceArray.reduceRightArray.someFunction.bindObject.keys

if(!Array.prototype.every)Array.prototype.every=function(fun,thisp){var len=this.length>>>0;if(typeof fun!="function")throw new TypeError;var thisp=arguments[1];for(var i=0;i<len;i++)if(i in this&&!fun.call(thisp,this[i],i,this))return false;return true}; if(!Array.prototype.filter)Array.prototype.filter=function(fun,thisp){var len=this.length>>>0;if(typeof fun!="function")throw new TypeError;var res=[];var thisp=arguments[1];for(var i=0;i<len;i++)if(i in this){var val=this[i];if(fun.call(thisp,val,i,this))res.push(val)}return res}; if(!Array.prototype.forEach)Array.prototype.forEach=function(fun,thisp){var len=this.length>>>0;if(typeof fun!="function")throw new TypeError;var thisp=arguments[1];for(var i=0;i<len;i++)if(i in this)fun.call(thisp,this[i],i,this)};if(!Array.prototype.indexOf)Array.prototype.indexOf=function(elt){var len=this.length>>>0;var from=Number(arguments[1])||0;from=from<0?Math.ceil(from):Math.floor(from);if(from<0)from+=len;for(;from<len;from++)if(from in this&&this[from]===elt)return from;return-1}; if(!Array.prototype.lastIndexOf)Array.prototype.lastIndexOf=function(elt){var len=this.length;var from=Number(arguments[1]);if(isNaN(from))from=len-1;else{from=from<0?Math.ceil(from):Math.floor(from);if(from<0)from+=len;else if(from>=len)from=len-1}for(;from>-1;from--)if(from in this&&this[from]===elt)return from;return-1}; if(!Array.prototype.map)Array.prototype.map=function(fun,thisp){var len=this.length>>>0;if(typeof fun!="function")throw new TypeError;var res=new Array(len);var thisp=arguments[1];for(var i=0;i<len;i++)if(i in this)res[i]=fun.call(thisp,this[i],i,this);return res}; if(!Array.prototype.reduce)Array.prototype.reduce=function(fun){var len=this.length>>>0;if(typeof fun!="function")throw new TypeError;if(len==0&&arguments.length==1)throw new TypeError;var i=0;if(arguments.length>=2)var rv=arguments[1];else{do{if(i in this){var rv=this[i++];break}if(++i>=len)throw new TypeError;}while(true)}for(;i<len;i++)if(i in this)rv=fun.call(undefined,rv,this[i],i,this);return rv}; if(!Array.prototype.reduceRight)Array.prototype.reduceRight=function(fun){var len=this.length>>>0;if(typeof fun!="function")throw new TypeError;if(len==0&&arguments.length==1)throw new TypeError;var i=len-1;if(arguments.length>=2)var rv=arguments[1];else{do{if(i in this){var rv=this[i--];break}if(--i<0)throw new TypeError;}while(true)}for(;i>=0;i--)if(i in this)rv=fun.call(undefined,rv,this[i],i,this);return rv}; if(!Array.prototype.some)Array.prototype.some=function(fun,thisp){var i=0,len=this.length>>>0;if(typeof fun!="function")throw new TypeError;var thisp=arguments[1];for(;i<len;i++)if(i in this&&fun.call(thisp,this[i],i,this))return true;return false}; if(!Function.prototype.bind)Function.prototype.bind=function(context){if(typeof this!=="function")throw new TypeError;var _arguments=Array.prototype.slice.call(arguments,1),_this=this,_concat=Array.prototype.concat,_function=function(){return _this.apply(this instanceof _dummy?this:context,_concat.apply(_arguments,arguments))},_dummy=function(){};_dummy.prototype=_this.prototype;_function.prototype=new _dummy;return _function}; Object.keys=Object.keys||function(o){var result=[];for(var name in o)if(o.hasOwnProperty(name))result.push(name);return result};