簡體   English   中英

在關閉彈出窗口中打開彈出窗口並刷新父頁面

[英]Open popup and refresh parent page on close popup

我在JavaScript中使用window.open打開了一個彈出窗口,我想在關閉這個彈出窗口時刷新父頁面。(onclose event?)我該怎么做?

window.open("foo.html","windowName", "width=200,height=200,scrollbars=no");

您可以使用' window.opener '訪問父窗口,因此,在子窗口中寫下以下內容:

<script>
    window.onunload = refreshParent;
    function refreshParent() {
        window.opener.location.reload();
    }
</script>

彈出窗口沒有您可以收聽的任何關閉事件。

另一方面,當窗口關閉時,有一個關閉的屬性設置為true。

您可以設置一個計時器來檢查已關閉的屬性,並執行以下操作:

var win = window.open('foo.html', 'windowName',"width=200,height=200,scrollbars=no");   
var timer = setInterval(function() {   
    if(win.closed) {  
        clearInterval(timer);  
        alert('closed');  
    }  
}, 1000); 

看到這個工作小提琴的例子

在您的子頁面上,放置以下內容:

<script type="text/javascript">
    function refreshAndClose() {
        window.opener.location.reload(true);
        window.close();
    }
</script>

<body onbeforeunload="refreshAndClose();">

但是作為一個好的UI設計,你應該使用一個關閉button因為它更加用戶友好。 見下面的代碼。

<script type="text/javascript">
    $(document).ready(function () {
        $('#btn').click(function () {
            window.opener.location.reload(true);
            window.close();
        });
    });
</script>

<input type='button' id='btn' value='Close' />

我用這個:

<script language='javascript'>
var t;
function doLoad() {
t = setTimeout("window.close()",1000);
}

</script>
<script type="text/javascript">
function refreshAndClose() {
    window.opener.location.reload(true);
    window.close();
}
</script>
<body onbeforeunload="refreshAndClose();" onLoad='doLoad()''>

當窗口關閉它然后刷新父窗口。

window.open將返回對新創建的窗口的引用,前提是打開的URL符合Same Origin Policy

這應該工作:

function windowClose() {
    window.location.reload();
}

var foo = window.open("foo.html","windowName", "width=200,height=200,scrollbars=no");
foo.onbeforeunload= windowClose;​

如果您的應用在支持HTML5的瀏覽器上運行。 您可以使用postMessage 這里給出的例子與你的非常相似。

在我的情況下,我點擊父頁面中的鏈接按鈕打開一個彈出窗口。 使用關閉子項刷新父級

window.opener.location.reload();

在子窗口導致重新打開子窗口(可能是因為查看狀態我猜。糾正我如果我錯了)。 所以我決定不在父級重新加載頁面並再次加載頁面為其分配相同的URL。

為了避免在關閉彈出窗口后再次打開彈出窗口,這可能有所幫助,

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

試試這個

        self.opener.location.reload(); 

打開當前窗口的父級並重新加載該位置。

您可以在完成所有操作的步驟之后使用父命令(父級是窗口)訪問主頁面...

    function funcx() {
        var result = confirm('bla bla bla.!');
        if(result)
            //parent.location.assign("http://localhost:58250/Ekocc/" + document.getElementById('hdnLink').value + "");
            parent.location.assign("http://blabla.com/" + document.getElementById('hdnLink').value + "");
    } 

您可以在父頁面中使用以下代碼。

<script>
    window.onunload = refreshParent;
    function refreshParent() {
      window.opener.location.reload();
    }
</script>

以下代碼將設置刷新父窗口后關閉:

function ManageQB_PopUp() {
            $(document).ready(function () {
                window.close();
            });
            window.onunload = function () {
                var win = window.opener;
                if (!win.closed) {
                    window.opener.location.reload();
                }
            };
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM