2010-11-23 59 views
1

我使用javascript window.open方法來打開讓說http://www.google.com [它始終將是一些外部URL]的JavaScript父頁面上的警報,當孩子彈出關閉

我都存儲在一個窗口對象的引用變量,問題是該變量永遠不會在父頁面上爲空,我不能提醒用戶彈出已關閉。

下面的代碼

 
    
     var winFB; 
     var winTWt; 
     var counterFB = 0; 
     var counterTWT = 0; 
     var timerFB; 
     function openFB() { 

      if (counterFB == 0) { 
       winFB = window.open("http://www.google.com"); 
       counterFB = 1; 
      } 
      if (counterFB > 0) { 
       alert(winFB); 
       if (winFB == null) { 
        counterFB = 0; 
        clearTimeout(timerFB); 
        alert("Window Closed"); 
       } 
      } 
      timerFB= setTimeout("openFB()", 1000); 
     } 

    

我不能把任何JavaScript代碼的彈出/子窗口。

希望有人能幫助我在此

回答

4

窗口變量不被清零了,當它關閉,但其.closed propertytrue,所以只要改變你的支票是該屬性,像這樣:

var winFB; 
var winTWt; 
var counterFB = 0; 
var counterTWT = 0; 
var timerFB; 
function openFB() { 
    if (counterFB == 0) { 
     winFB = window.open("http://www.google.com"); 
     counterFB = 1; 
    } 
    if (counterFB > 0) { 
     if (winFB.closed) { 
      counterFB = 0; 
      clearTimeout(timerFB); 
      alert("Window Closed"); 
     } 
    } 
    timerFB = setTimeout(openFB, 1000); 
} 

還要注意setTimeout()變化,通過在儘可能(幾乎總是),而不是一個字符串,你有作用域少了很多問題的功能。

1
//in the parent 
winFB = window.open("http://www.google.com"); 

//in the child window you can access 
var _parent = window.opener.document; 

//also use the onunload event in the child window 
onunload="doSomething()" 

希望這個給你一些方向..

+0

我不能把兒童窗口 – Sandhurst 2010-11-23 10:17:43

相關問題