2013-10-16 84 views
0

我想提出一個有兩個模式的Chrome擴展,並且每當圖標點擊彈出應該出現消息,解釋到他是在什麼模式下,用戶。JavaScript的彈出消息僅出現一次

問題是該消息不會始終顯示(彈出窗口本身被打開)。

我試過Google搜索它,但似乎沒有很多人遇到過這個問題。

任何人都可以幫忙嗎?

這裏是我的代碼:

function openWin1() 
{ 
    myWindow=window.open('','','width=500,height=500,toolbar=0, location=0, status=0, menubar=0, scrollbars=0, resizable=0'); 
    myWindow.moveTo(500,100); 
    myWindow.document.write("<p>mode1</p>"); 
    myWindow.focus(); 
} 

function openWin2() 
{ 
    myWindow=window.open('','','width=500,height=500,toolbar=0, location=0, status=0, menubar=0, scrollbars=0, resizable=0'); 
    myWindow.moveTo(500,100); 
    myWindow.document.write("<p>mode2</p>"); 
    myWindow.focus(); 

} 


chrome.browserAction.onClicked.addListener(onClickExtensionIcon); 
function onClickExtensionIcon(tab) 
{ 
    if(hasAPrise) 
    { 
     hasAPrise = false; 
     chrome.browserAction.setIcon({path: '/icon.png'}); 
     openWin1(); //open the first popup 
    } 
    else 
    { 
     openWin2(); //open the second popup 
    } 


} 

在此先感謝

編輯:我注意到,如果我最大化和幾次儘量減少彈出,消息確實出現。另外,當我檢查彈出源時,它的消息是正確的。這很有趣。任何想法如何解決這一問題。

+0

沒有得到顯示什麼信息?我在代碼中看不到一個。 – Moob

+0

'myWindow.document.write(「

mode1

」);和myWindow.document.write(「

mode2

」);'當打開第一個彈出窗口應該說mode1,第二個應該說mode2 – r3x

回答

1

你必須寫它後關閉文檔:

myWindow.document.write("<p>mode2</p>"); 
myWindow.document.close(); 

UPDATE:這也可能是有用的命名你的目標,這樣你就不會在每次鏈接時產生一個新的窗口點擊,也許用html包裝你的內容。

function openWin1(){ 
    myWindow=window.open('','popup1','width=500,height=500,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0'); 
    myWindow.moveTo(500,100); 
    myWindow.document.write("<html><head><title>Popup1</title></head><body>I am popup 1</body></html>"); 
    myWindow.document.close(); 
    myWindow.focus(); 
    myWindow.resizeTo(499,499); //UPDATE: force resize because OP is getting improved results after window resize 
} 

我不能對此進行測試作爲一個實際的瀏覽器擴展程序,但我已經a JSFiddle showing it working as html/js

+0

感謝或答案,但它不起作用。任何其他建議:( – r3x

+0

是的,看我的更新 – Moob

+0

試過了,但它沒有工作。似乎由於某種原因,只有在我重新調整窗口大小或查看源代碼後才加載文本。 – r3x