我需要在父窗口中显示警报,然后在关闭子窗口时刷新它。我尝试过:
window.onunload = refreshParent;
function refreshParent() {
window.opener.focus();
window.opener.alert('Testing');
window.opener.location.reload();
}
这确实在父窗口中显示了警报,但我猜因为子窗口位于父窗口中,所以子窗口 js 会卡住,直到我在父窗口中点击“确定”。我可以忍受这一点,但问题是,当我显示警报时,父窗口似乎没有获得焦点,因此警报最终隐藏在弹出窗口后面。大家有什么建议吗?
最佳答案
试试这个代码
<!DOCTYPE html>
<html>
<head>
<script>
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
refreshParent();
}
function refreshParent() {
window.parent.focus();
window.parent.alert("message");
window.parent.location.reload();
}
</script>
</head>
<body>
<input type="button" value="Open 'myWindow'" onclick="openWin()" />
</body>
</html>
关于javascript - 如何在 JavaScript 中从子弹出窗口在父窗口中显示警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17440200/