gpt4 book ai didi

javascript - window.opener 返回 null

转载 作者:行者123 更新时间:2023-12-02 14:39:38 24 4
gpt4 key购买 nike

我在 www.abc.com 中有一个链接,单击该链接会打开 www.def.com 的弹出窗口。该弹出窗口中有一个来自 www.def.com 的表单。单击表单上的“保存”按钮后,我希望关闭当前窗口并且父级应该重定向到一个位置。

在此更改之前,当我显示 www.abc.com 的表单时,以下代码运行良好。

<script language="JavaScript">        
parent.window.close();
parent.opener.parent.window[1].location.replace('/abc.jsp?custid=12345');
</script>

但是现在“parent.opener”返回null。所以我可以关闭弹出窗口,但无法将父窗口重定向到所需位置。

我知道我的要求有些过分,但这就是要求。

最佳答案

在“abc.moc”

<!DOCTYPE html>
<html>
<head>
<script>
// open `popup`
var popup = window.open("popup.html", "popup", "width=200,height=200");
// handler `message` event from `popup.html`
function receiveMessage(event) {
console.log(event, event.data);
this.location.href = event.data;
}
window.addEventListener("message", receiveMessage, false);
</script>
</head>
<body>
</body>
</html>

在“def.moc”

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form>
<input type="button" value="Save">
</form>
<script>
console.log(window.opener);
var button = document.querySelector("form input[type=button]");
button.onclick = function(e) {
e.preventDefault();
e.stopPropagation();
// do stuff with `form`
// call `.postMessage()` on `window.opener` with
// first parameter URL to redirect `abc.moc`,
// second parameter `location.href` of `abc.moc`
window.opener.postMessage("redirect.html",
window.opener.location.href);
// close `popup`
window.close();
}
</script>
</body>
</html>

plnkr http://plnkr.co/edit/pK4XBJDrqFrE7awvMlZj?p=preview

关于javascript - window.opener 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37127985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com