我在www.abc.com中有一个链接,点击它可以从www.def.com打开一个弹出窗口。在www.def.com的弹出窗口中有一个表单。单击表单上的"Save“按钮后,我希望关闭当前窗口,父窗口应该重定向到某个位置。
在此更改之前,当我显示来自www.abc.com的表单时,下面的代码运行良好。
<script language="JavaScript">
parent.window.close();
parent.opener.parent.window[1].location.replace('/abc.jsp?custid=12345');
</script>但现在是"parent.opener" is returning null。因此,我能够关闭弹出窗口,但不能将父窗口重定向到已删除的位置。
我知道我要求的是有点有线,但这是要求。
发布于 2016-05-10 03:56:10
在"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
https://stackoverflow.com/questions/37127985
复制相似问题