1>window.opener 的用法
在一般的用法中,只是用來解決關閉窗口時不提示彈出窗口, 而對它更深層的瞭解一般比較少。其實 window.opener是指調用window.open方法的窗口。 在工作中主要是用來解決部分提交的。這種跨頁操作對工作是非常有幫助的。 如果你在主窗口打開了一個頁面,並且希望主窗口刷新就用這個,打開頁面的window.opener就相當於 主窗口的window。 主窗口的刷新你可以用 window.opener.location.reload(); 如果你用虛擬的目錄:如struts的*.do會提示你重試
你可以改成這樣 window.opener.yourformname.submit() 就好了
2〉 在應用中有這樣一個情況, 在A窗口中打開B窗口,在B窗口中操作完以後關閉B窗口,同時自動刷新A窗口
function closeWin(){ hasClosed = true; window.opener.location="javascript:reloadPage();"; window.close(); } function window.onbeforeunload(){ if(!hasClosed){ window.opener.location="javascript:reloadPage();"; } }
</script> 上面的代碼在關閉B窗口的時候會提示錯誤,說缺少Object,正確的代碼如下: function closeWin(){ hasClosed = true; window.opener.location="javascript:reloadPage();"; window.opener=null; window.close(); } function window.onbeforeunload(){ if(!hasClosed){//如果已經執行了closeWin方法,則不執行本方法 window.opener.location="javascript:reloadPage();"; } }
</script> reloadPage方法如下: function reloadPage() { history.go(0); document.execCommand("refresh") document.location = document.location; document.location.reload(); } PS:由於需要支持正常關閉和強制關閉窗口時能捕捉到事件,用了全局變量hasClosed
==============================================
補充,在父窗口是frame的時候在刷新父窗口的時候會出現問題:
The page cannot be refreshed without resending the information. 後修改如下: window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href; 不需要執行自帶的reload()方法,注意,不要再畫蛇添足加上這一句:
window.opener.parent.document.frames.item('mainFrame').location.reload();
======================================================================================== 最後,爲了同時支持刷新普通父窗口和frame父窗口,代碼如下: function closeWin() { hasClosed = true; <%if(null != frame){%> window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href; <%}else{%> window.opener.location = "javascript:reloadPage();"; <%}%> //window.opener.top.mainFrame.location="javascript:reloadPage();"; //self.opener.frames.mainFrame.location.reload(true); window.opener = null; window.close(); } function window.onbeforeunload(){ if (!hasClosed) { <%if(null != frame){%> window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href; <%}else{%> window.opener.location = "javascript:reloadPage();"; <%}%> window.opener = null; } }
|