关于javascript:window.opener返回null

window.opener is returing null

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

在进行此更改之前,当我从www.abc.com显示表单时,以下代码可以正常工作。

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

但是现在"parent.opener" is returning null。 所以我可以关闭弹出窗口,但不能将父窗口重定向到
位置不理想。

我知道我要问的是有线的,但这是必需的。


在" abc.moc"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
 
    // 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);
 
</head>
<body>
</body>
</html>

在" def.moc"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <form>
    <input type="button" value="Save">
  </form>
   
    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();
    }
   
</body>
</html>

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