2011-05-15 88 views
1

在Firefox 4.0或Internet Explorer 8+中不會發生此錯誤。谷歌瀏覽器未被捕獲的javascript異常

我創建一個新的空標籤,並打開控制檯(按Ctrl + Shift + I,然後按Esc),並粘貼以下代碼:

var cnt = 0; 
(function() {document.body.innerHTML = cnt;window.setTimeout(arguments.callee,100);})(); 

(function(){cnt++;window.setTimeout(arguments.callee,0);})(); 

有時,在這一點上的錯誤,但並非總是如此。

在此之後我貼得更:

(function(){cnt++;window.setTimeout(arguments.callee,0);})(); 

後〜30秒我得到任何這樣的錯誤:

Uncaught TypeError: Cannot read property 'offsetHeight' of null
Uncaught TypeError: Cannot read property 'classList' of null

的問題:是什麼問題?我該如何解決這個問題?

UPDATE:

這個錯誤,當我打開我的選項卡之間切換髮生,但也隨機。

回答

1

這是因爲chrome://newtab預計某些元素在那裏(爲了JavaScript的目的),但你通過使用document.body.innerHTML來清除它們。你可以注入一個元素來代替你的輸出,像這樣的東西應該可以工作:

var cnt = 0; 
document.body.innerHTML = '<div id="abcd">...</div>' + document.body.innerHTML; 

(function(){ 
    document.getElementById('abcd').innerHTML=cnt; 
    window.setTimeout(arguments.callee,100); 
})(); 

(function(){ cnt++;window.setTimeout(arguments.callee,0); })();