首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >window.opener正在重传null

window.opener正在重传null
EN

Stack Overflow用户
提问于 2016-05-10 01:42:10
回答 1查看 5.5K关注 0票数 0

我在www.abc.com中有一个链接,点击它可以从www.def.com打开一个弹出窗口。在www.def.com的弹出窗口中有一个表单。单击表单上的"Save“按钮后,我希望关闭当前窗口,父窗口应该重定向到某个位置。

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

代码语言:javascript
复制
<script language="JavaScript">        
  parent.window.close();
  parent.opener.parent.window[1].location.replace('/abc.jsp?custid=12345');
</script>

但现在是"parent.opener" is returning null。因此,我能够关闭弹出窗口,但不能将父窗口重定向到已删除的位置。

我知道我要求的是有点有线,但这是要求。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-10 03:56:10

在"abc.moc“

代码语言:javascript
复制
<!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“

代码语言:javascript
复制
<!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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37127985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档