时间:2021-07-01 10:21:17 帮助过:4人阅读
var isWindow = function(obj){
return obj.window === obj.window.window
}
======================华丽的分界线=====================
感谢 Ivony提供的灵感!
代码如下:
function isWindow( obj ){
if(typeof obj !== "object") return false;//必须是一个对象
var expando = "dom"+(new Date-0) //生成一个随机变量名
//全局解析代码,IE的eval只对原作用域有效
//详见http://www.javaeye.com/topic/519098
//加之eval与with是 html5严格模式下要禁止的东西,弃之不用!
var js = document.createElement("script");
var head = document.getElementsByTagName("head")[0];
head.insertBefore(js,head.firstChild);
js.text = expando + " = {};"
head.removeChild(js)
return window[expando] === obj[expando]
}
isWindow(window)