打开弹出窗口,并在关闭弹出窗口时刷新父页面

我通过window.open打开了一个弹出窗口。在JavaScript中打开,我想在关闭此弹出窗口时刷新父页面。(onclose事件?)我该怎么办?


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


收到一只叮咚
浏览 653回答 3
3回答

潇潇雨雨

您可以使用“ window.opener ” 访问父窗口,因此,在子窗口中编写如下内容:<script>&nbsp; &nbsp; window.onunload = refreshParent;&nbsp; &nbsp; function refreshParent() {&nbsp; &nbsp; &nbsp; &nbsp; window.opener.location.reload();&nbsp; &nbsp; }</script>

小怪兽爱吃肉

在您的子页面上,输入以下内容:<script type="text/javascript">&nbsp; &nbsp; function refreshAndClose() {&nbsp; &nbsp; &nbsp; &nbsp; window.opener.location.reload(true);&nbsp; &nbsp; &nbsp; &nbsp; window.close();&nbsp; &nbsp; }</script>和<body onbeforeunload="refreshAndClose();">但作为一个好的UI设计,您应该使用Close,button因为它更加用户友好。参见下面的代码。<script type="text/javascript">&nbsp; &nbsp; $(document).ready(function () {&nbsp; &nbsp; &nbsp; &nbsp; $('#btn').click(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.opener.location.reload(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.close();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script><input type='button' id='btn' value='Close' />
打开App,查看更多内容
随时随地看视频慕课网APP