IE 中的 JavaScript 兼容性

发布于 2024-09-20 00:55:56 字数 123 浏览 3 评论 0原文

为什么我的 Javascript 小部件在 IE 上无法正常工作,但在 Firefox 上正常工作?此外,Firebug 不会产生任何错误。我该如何确保我的 Javascript 小部件可以在 IE 中运行?有什么工具可以帮助我吗?

Why is it that my Javascript widget does not work properly on IE but fine on Firefox? Moreover, Firebug doesn't produce any errors. What can I do to make sure my Javascript widget is operational in IE? Are there any tools to help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白云不回头 2024-09-27 00:55:56

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!
         };

A common problem with JS in IE is trailing commas in object and array literals: IE chokes and dies. All other browsers are fine. So look for:

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

an_obj = {foo: "This is foo",
          bar: "This is bar",  // Trailing comma kills IE!
         };
不甘平庸 2024-09-27 00:55:56

看来是IE的兼容性问题。您可以在右下角查找标准 JavaScript 错误警报图标。另外,IE开发工具栏很有帮助,但不如Firebug好用。最坏的情况是,开始抛出一些警报,直到找到断点。

如果您使用console.log,那么在黑暗中进行尝试,在其他浏览器中将会失败。作为一名开发人员,我之前已经把它留下了。

Seems like a compatibility issue with IE. You could look in the lower right for the standard JavaScript error alert icon. In addition, IE Developer Toolbar is helpful, but not as nice as Firebug. Worst case, start throwing some alerts until you find the breakpoint.

Just a stab in the dark, if you are using console.log, that will fail in other browsers. As a developer, I've left that in before.

清醇 2024-09-27 00:55:56

IE 6+ 都很好地符合 ECMA 规范(基本上涵盖了 Javascript 的所有核心“programmey”对象,例如日期、数学和数组对象 - 任何涉及数学或数据类型的对象)。但是,如果您正在处理任何涉及标准 W3C DOM 的内容(那些与访问 HTML 文档的任何部分或其中触发的事件相关的对象),那么您的函数很可能会在 IE 中崩溃,而 IE 一直落后于 DOM 规范十多年来。整个框架的建立是为了弥补这一点。如果您正在处理事件或访问 HTML 元素或其属性,您将需要使用 JQuery 之类的框架,或者开始阅读一些有关 JavaScript 的书籍来了解需要分支的对象和属性。

另一件需要记住的事情是,所有浏览器制造商都通过实验添加自己的专有方法。因此,Firefox 的非标准但非常流行的console.log。公平地说,MS(我仍然觉得他们很卑鄙),他们的 XMLHttpRequest 对象的原始版本是所有这些 Ajax 东西的孵化器,他们还给了我们innerHTML,它不是任何标准的一部分,但被采用并且工作原理相同所有浏览器。

基本上,所有浏览器都会解析和解释自己版本的 JavaScript。您需要学习所有在所有方面都具有相同作用的常见部分,以及如何处理他们都不同意的事情。

书籍:我推荐 Jeremy Keith 的 DOM Scripting,然后是 O'Reilly 的巨著(我也喜欢 Osbourne 的巨著 Complete Reference)。

站点: Quirksmode.org 的内容似乎比以前少了,但仍然有很多关于编写核心 JS 的好建议,以弥补 IE 的不足。 CSS 上也有很多东西。

IEs 6+ all conform pretty well to the ECMA spec (essentially covers all the core 'programmey' objects of Javascript like the Date, Math, and Array objects -- anything dealing with Math or data types). However, if you're dealing with anything that touches the standard W3C DOM (those objects that relate to accessing any part of an HTML document or events fired therein) it's most likely your functions will kerplode in IE which has been lagging behind the DOM spec for over ten years. Entire frameworks have been built to compensate for this. If you're dealing with events or accessing HTML elements or their attributes you're going to want to use a framework like JQuery or start reading some books on JavaScript to learn what objects and properties you need to branch for.

Another thing to keep in mind is that that all of the browser manufacturers add their own proprietary methods by way of experimentation. Thus, Firefox's nonstandard but very popular console.log. To be fair to MS (who I still find despicable), their original version of the XMLHttpRequest object is what hatched all of this Ajax stuff and they also gave us innerHTML which is not a part of any standard but was adopted and works the same in all browsers.

Basically, all the browsers parse and interpret their own versions of JavaScript. It's up to you to learn all the common bits that work the same across the board and how to go about dealing with the stuff none of them agree on.

Books: I recommend Jeremy Keith's DOM Scripting and then the big giant O'Reilly book (I also like the big giant Complete Reference book from Osbourne).

Sites: Quirksmode.org seems to have less content than it used to but still has a lot of good advice on writing core JS to compensate for IE incompetence. Lots of stuff on CSS too.

醉城メ夜风 2024-09-27 00:55:56

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

Open the widget in IE8 and use the lame(compared to Firebug) developer toolbar that comes with it(Keyboard shortcut: F12).

木落 2024-09-27 00:55:56

遗憾的是,JavaScript 在所有浏览器中的工作方式并不完全相同。你几乎只需要调试它。

请参阅 http://blogs.msdn.com/b /ie/archive/2004/10/26/247912.aspx 讨论了可以充当 IE JavaScript 调试器的三种不同工具。

Sadly, JavaScript doesn't work exactly the same in all browsers. You pretty much just need to debug it.

See http://blogs.msdn.com/b/ie/archive/2004/10/26/247912.aspx for a discussion of three different tools that can act as a debugger for IE JavaScript.

找回味觉 2024-09-27 00:55:56

也许您需要从 MDC 添加兼容性算法,

这里是缩小版本Array.every、Array.filter、Array.forEach、Array.indexOf、Array.lastIndexOf 、Array.mapArray.reduceArray.reduceRightArray.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};

maybe you need to add compatibility algorithm from MDC

here is the minified version of 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};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文