为什么我的Javascript小部件在IE上无法正常工作,但在Firefox上很好?而且,Firebug不会产生任何错误。我能做些什么来确保我的Javascript小部件在IE中运行?有什么工具可以帮助我吗?IE中的Javascript兼容性
回答
似乎与IE的兼容性问题。您可以在右下角查看标准JavaScript错误警报图标。另外,IE Developer Toolbar很有用,但不如Firebug。最坏的情况下,开始投掷一些alerts
,直到找到断点。
只是在黑暗中刺中,如果您使用的是console.log
,那么在其他浏览器中会失败。作为一名开发人员,我之前已经离开过。
在IE8中打开小部件并使用它自带的跛脚(与Firebug相比)开发人员工具栏(键盘快捷键:F12)。
不幸的是,JavaScript在所有浏览器中的工作方式都不尽相同。你几乎只需要调试它。
查看http://blogs.msdn.com/b/ie/archive/2004/10/26/247912.aspx讨论三种不同的工具,它们可以充当IE JavaScript的调试器。
在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!
};
+1:我自己使用脚本在签入代码前使用regexp来检查它。我有时会得到误报,但这比我的老板在凌晨2点打电话给我更好地修复损坏的更新要好得多。 – slebetman 2010-09-17 03:35:47
我不是IE的爱好者,但这是IE为改变做正确事情的典型例子。让坏代码破坏。 – 2011-06-22 07:32:52
的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的东西。
也许你需要从MDC
添加兼容算法这里是Array.every
,Array.filter
,Array.forEach
,Array.indexOf
,Array.lastIndexOf
,Array.map
,Array.reduce
,Array.reduceRight
,Array.some
,Function.bind
,Object.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};
- 1. IE/jQuery/CSS的兼容性
- 2. ie 8兼容性问题
- 3. IE兼容性问题
- 4. IE中的设计兼容性
- 5. JavaScript中的非IE浏览器兼容性所需的帮助
- 6. JavaScript兼容性库
- 7. IE JS兼容性 - 在FF中工作?
- 8. React flexbox与ie 10的兼容性
- 9. 的CSS问题,IE/FF兼容性
- 10. 的JavaScript的indexOf VS Array.prototype.indexOf IE兼容性错误
- 11. IE兼容模式JavaScript函数不叫
- 12. Javascript开始与iPhone和IE不兼容
- 13. JavaScript和Flash与IE不兼容
- 14. javascript兼容性问题
- 15. Javascript IN操作兼容性
- 16. Javascript/jquery兼容性问题
- 17. JavaScript框架IE6兼容性
- 18. IE兼容性问题w/wordpress
- 19. Codemirror(textarea replace)IE兼容性问题
- 20. IE表行跨度兼容性
- 21. IE浏览器兼容性问题
- 22. jQuery浏览器兼容性(IE)
- 23. IE兼容性模式问题
- 24. IE跨浏览器兼容性问题
- 25. IE兼容性模式问题
- 26. IE 8和IE 7的兼容性问题
- 27. Internet Explorer 8的旧javascript兼容性
- 28. JavaScript的不兼容性问题与IE8
- 29. 这里似乎有一些IE不兼容的javascript。有没有浏览器兼容性专家?
- 30. 排序与IE不兼容?
缩小的版本检查你没有粘贴的代码的第34行第5列。 – MooGoo 2010-09-17 02:28:02
苛刻** MooGoo **,但它确实让我LOL。 – 2010-09-17 02:31:16
这使我成为了LOL,但我想我正在寻找更多关于解决问题的一般答案。例如,IE浏览器的Firebug工具? – syker 2010-09-17 03:19:50