Internet Explorer 7/8和窗口函数是空对象 [英] Internet Explorer 7/8 and window functions are empty objects

查看:66
本文介绍了Internet Explorer 7/8和窗口函数是空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Internet Explorer 8中(在IE7 / 8模式下也适用于IE9),以下代码提醒 object undefined 而不是预期的函数和类似 function(){[native code]}

  alert(typeof window.setTimeout =+ typeof window.setTimeout); // object 
alert(window.setTimeout.apply =+ window.setTimeout.apply); // undefined

试一试: http://jsfiddle.net/BsvZw/5/



为什么会发生这种情况?获得实际 setTimeout 的解决方法是什么?



更新



我正在尝试围绕创建一个包装器setTimeout

  var _oldSetTimeout = window.setTimeout; 
window.setTimeout = function()
{
// ...

返回_oldSetTimeout.apply(this,arguments); //这是IE 7/8说'对象不支持这个属性或方法'的地方'
//和_oldSetTimeout看起来像一个空对象
};


解决方案


为什么会发生这种情况?


基本上,因为IE讨厌网络开发人员并且正在搞乱你。



<更严重的是,浏览器实现提供的不属于核心Javascript语言的内容可能被归类为 host对象。当涉及到宿主对象时,所有的赌注都是关闭的,他们基本上可以做任何他们想要的事情 [1] ,而不需要遵守通常的Javascript语义。


获取实际setTimeout的解决方法是什么?


我知道它真的很难看,但你可以做一个if-else-if链到预定义数量的参数。在setTimeout的情况下,这应该不是一个大问题,因为你不需要超过2或3个参数。

  var _oldSetTimeout = window.setTimeout; 
window.setTimeout = function(a1,a2,a3)
{
switch(arguments.length){
case 0:return _oldSetTimeout();
case 1:return _oldSetTimeout(a1);
case 2:return _oldSetTimeout(a1,a2);
default:return _oldSetTimeout(a1,a2,a3);
}
};

虽然这是一个非常难看的解决方案,但有时它是唯一的方法。例如,无法使用可变参数调用构造函数。






[1]为了让您了解邪恶的主机对象如何,前几天我不得不对DOM节点/文档中的XPath方法进行特征检测。而不是通常的 if(node.selectNodes)测试我必须使用 if(节点中的selectNodes)因为IE中的节点是主机对象,只是访问selectNodes属性实际上会调用它,给我一个参数数量不正确的异常!


In Internet Explorer 8 (works also in IE9 in IE7/8 modes) the following code alerts object and undefined instead of expected function and something like function() { [native code] }.

alert("typeof window.setTimeout = " + typeof window.setTimeout);  // object
alert("window.setTimeout.apply  = " + window.setTimeout.apply );  // undefined

Try it: http://jsfiddle.net/BsvZw/5/

Why is this happening? What would be a workaround to get the actual setTimeout?

Update

I am trying to create a wrapper around setTimeout:

var _oldSetTimeout = window.setTimeout;
window.setTimeout = function ()
{
    // ...

    return _oldSetTimeout.apply(this, arguments);    // this is place where IE 7/8 says 'Object doesn't support this property or method'
                                                // and _oldSetTimeout looks like an empty object
};

解决方案

Why is this happening?

Basically, because IE hates web developers and is messing with you.

More seriously, things provided by the browser implementation that are not part of the core Javascript language may be classified as host objects. When it comes to host objects all bets are off and they are basically allowed to do anything they want[1] without needing to respect usual Javascript semantics.

What would be a workaround to get the actual setTimeout?

I know its really ugly, but you could do an if-else-if chain up to a predefined number of arguments. In setTimeout's case this shouldn't be a big problem since you shouldn't ever need more then 2 or 3 arguments for it.

var _oldSetTimeout = window.setTimeout;
window.setTimeout = function (a1, a2, a3)
{
   switch(arguments.length){
       case 0:  return _oldSetTimeout();
       case 1:  return _oldSetTimeout(a1);
       case 2:  return _oldSetTimeout(a1, a2);
       default: return _oldSetTimeout(a1, a2, a3);
   }
};

While this is a very ugly solution, sometimes its the only way. For example, there is no way to invoke constructors with variadic arguments either.


[1] To give you an idea of how evil host objects can be, the other day I had to do feature detection for XPath methods in DOM nodes/documents. Instead of the usual if(node.selectNodes) test I had to use if("selectNodes" in node) because nodes are host objects in in IE and just accessing the selectNodes property would actually call it, giving me an "incorrect number of arguments" exception!

这篇关于Internet Explorer 7/8和窗口函数是空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆