2011-09-29 68 views
1

在我的Firefox插件中。 我有兩個選項卡中打開在我的瀏覽器,當我嘗試訪問「文檔」間隔/超時後,在其中一個選項卡,我搶,而不是另一個選項卡的文件...文檔被用作其他選項卡

例如: 標籤1,document.title是:「Test page」 Tab 2,document.title is:「Second tab」

我在標籤1中運行腳本:setTimeout(function(){alert(document.title)} ,5000)。 應提醒「測試頁」,但警報顯示「第二個標籤」。

這裏我的腳本:

gBrowser.addEventListener("DOMContentLoaded",function(e){ 
    window = e.originalTarget.defaultView; 
    document = window.document; 
    setTimeout(function(){ alert(document.title); }, 5000); 
}, true); 

這隻有當我打開第一個選項卡happends,然後打開第二。

當我嘗試更改任何dom元素時,會發生同樣的事情。

當用戶點擊一個按鈕時也會發生。

如何避免這種情況? 這可能是一個Firefox的錯誤或與我?

回答

1

不要忘記declare local variables

gBrowser.addEventListener("DOMContentLoaded",function(e){ 
    var window = e.originalTarget.defaultView; 
    var document = window.document; 
    setTimeout(function(){ alert(document.title); }, 5000); 
}, true); 

未聲明的變量是自動全球,特別是現在,當之間更改超時運行(不去管很多其他討厭的副作用)。更好的是:switch on strict mode。它會確保這個錯誤產生一個明顯的錯誤,不會被忽視。

+0

謝謝,我沒有聲明變量。這有幫助。我用事件解決了這個問題:'function(e){var document = e.originalTarget.ownerDocument;}'這是最糟糕的問題(你點擊一個標籤上的按鈕,事件發生在第二個標籤中) – StiveKnx

相關問題