简体   繁体   English

window.opener正在重用null

[英]window.opener is returing null

I have a link in www.abc.com , clicking on which it opens up a popup from www.def.com . 我在www.abc.com有一个链接,单击该链接会从www.def.com弹出一个弹出窗口。 There is a form in that popup from www.def.com . www.def.com弹出窗口中有一个表单。 After clicking upon the "Save" button on the form I want the current window to be closed and the parent will should redirect to a location. 单击表单上的“保存”按钮后,我希望关闭当前窗口,并且父级将重定向到某个位置。

Before this change when I was showing the form from www.abc.com, the below code was working fine . 在进行此更改之前,当我从www.abc.com显示表单时,以下代码可以正常工作。

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

But now "parent.opener" is returning null . 但是现在"parent.opener" is returning null So I am able to close the popup but not able to redirect the parent window to disired location. 因此,我能够关闭弹出窗口,但无法将父窗口重定向到所需的位置。

I know what I am asking is bit wired, But this the requirement. 我知道我要问的是有线的,但这是必需的。

At "abc.moc" 在“ 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>

at "def.moc" 在“ 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 plnkr http://plnkr.co/edit/pK4XBJDrqFrE7awvMlZj?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM