2013-04-16 91 views
9

这里有来自第三方的lib中提取的有罪片段:类型错误:无法读取空的特性“控制台”

function hasConsoleLogger() { 
    return window.console && window.console.log; 
} 

没什么特别,但令人惊讶的返回: TypeError: Cannot read property 'console' of null

它在浏览器中执行上下文(Chrome),因此不涉及Node.js的非窗口内容。

我检查了潜在的恶意delete windowwindow = null没有成功。

发生此错误的应用程序在Friendly iFrames和document.write()调用中运行。

不幸的是,我无法提供任何问题的演示链接。

所以我想我的问题是“在浏览器中,窗口对象怎么可能被无效或无法访问呢?”

+0

我只是试图做:'窗口= NULL;';它在Chrome中静默失败。国际海事组织,它足够聪明,可以防止这种转让。 – SuperSaiyan

+0

它在全局范围内还是在一个函数内? – user2264587

回答

4

当窗口关闭时,Chrome首先设置window.consolenull,然后window

下面的代码将可靠地重现该错误,通过创建从一个不同的上下文引用window的函数:

var w = window.open('/'); 
// Using document.write to run code in the context of the other window 
w.document.write('<script>opener.func = function(){return window;};</script>'); 
w.close(); 
// Naive timer to wait for Garbage collection 
setTimeout(function() { 
    console.log(func() === null); // true 
}, 100); 
相关问题