2012-07-09 190 views
9

我正在創建一個轉到hello.html的彈出窗口。當我關閉彈出窗口(hello.html)時,我想讓我的原始(父頁面)重新加載。我似乎無法讓它工作,但我很接近。這裏是我的代碼至今主網頁和網頁hello.html的....關閉彈出窗口後刷新父窗口

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
function open_win() 
{ 
window.open("hello.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400"); 
} 
</script> 


<script language="JavaScript"> 

function refreshParent() { 
    window.opener.location.href = window.opener.location.href; 

    if (window.opener.hello.html) 

{ 
    window.opener.hello.html.close() 
    } 
    window.close(); 
} 
</script> 


</head> 

<body> 

<script type="text/javascript"> 

var d=new Date(); 
document.write(d); 

</script> 

<form> 
<input type="button" value="Open Window" onclick="open_win()"> 
</form> 
</body> 
</html> 

這裏是hello.html的...

<!DOCTYPE html> 
<html> 
<head> 
<script type="text/javascript"> 
</script> 
</head> 
<body> 

Hello 

</body> 
</html> 
+0

請檢查此:http://stackoverflow.com/questions/18321323/submit-form-reload-parent-and-close-child/36855748#36855748可能會幫助某人 – NoNaMe 2016-04-26 05:17:05

回答

11

訂閱unload event在子窗口並從子窗口調用父窗口來通知它正在關閉!

編輯加了代碼示例...

function popupClosing() { 
    alert('About to refresh'); 
    window.location.href = window.location.href; 
} 

var w = window.open("hello.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400"); 
w.onunload = function() { 
    window.parent.popupClosing() 
}; 
+0

我不知道這意味着什麼... – 2012-07-09 15:40:09

+0

謝謝..更好:) – 2012-07-09 16:06:16

3

像這樣做,創建彈出監控其「關閉」狀態屬性中的時間間隔後。但是,這是父文檔中添加:

var pop = window.open("page.html", "popup", 
      "width=800,height=500,scrollbars=0,title='popup'"); 
    pop.focus(); 

    var monitor = setInterval(function() { 

     if (pop.closed) { 
      document.location.reaload() 
     } 

    }, 1000); 
+0

這是一個相當不好的解決方案,但它是唯一一個在Chrome 55.0.2883.87中爲我工作的人。所有其他方法在onclick事件中都可以正常工作,這讓我相信它將Chrome限制爲僅由用戶啓動的類似調整大小和移動窗口的操作。 – Shaun 2017-01-19 16:00:22

2

嘗試把這段JavaScript代碼在彈出的窗口中:當您關閉彈出窗口

window.onunload = function(){ 
    window.opener.location.reload(); 
}; 

/onunload事件將觸發,和window.opener .location.reload()將重新加載源(父)窗口。/

1

上彈出關閉功能嘗試的click事件...

window.opener.location.reload(true); 
0

這段代碼,如果一個新的窗口與window.open功能彈出的工作爲好。

setTimeout(function() { window.opener.location.reload(true); window.close();}, 3000); 
0

將這個腳本在彈出窗口的標題:當您關閉彈出

<script type='text/javascript'> 
window.onunload = function(){ 
window.opener.location.reload();} 
</script> 

這將刷新父窗口。