我正在创建一个转到 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>
在子窗口中订阅unload事件,并从子窗口中调用父窗口来通知它正在关闭!
编辑添加了代码示例...
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()
};
这样做,在创建弹出监视器后,在一段时间内其“关闭”状态属性。但这是在父文档中添加的:
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);
尝试将此 JavaScript 代码放入弹出窗口中:
window.onunload = function(){
window.opener.location.reload();
};
当您关闭弹出窗口时,/onunload 事件将触发,window.opener.location.reload() 将重新加载源(父)窗口。/
弹出关闭功能的单击事件尝试...
window.opener.location.reload(true);
如果弹出带有
window.open
功能的新窗口,这段代码也同样有效。
setTimeout(function () { window.opener.location.reload(true); window.close();}, 3000);
将此脚本放入弹出窗口的标题中:
<script type='text/javascript'>
window.onunload = function(){
window.opener.location.reload();}
</script>
当您关闭弹出窗口时,这将重新加载父窗口。
<script type="text/javascript">
if ($("#<%=hfldClose.ClientID%>").val() === "Close") { // create hfldClose as hidden field and assign value by backend
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
layerRefresh();
window.parent.layerRefresh();
}
function layerRefresh() {
window.parent.location.href = "your Routing url";
}
</script>