opener正在重用null

opener正在重用null

本文介绍了window.opener正在重用null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a link in www.abc.com , clicking on which it opens up a popup from www.def.com .There is a form in that popup from www.def.com. After clicking upon the "Save" button on the form I want the current window to be closedand the parent will should redirect to a location.

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

Before this change when I was showing the form from www.abc.com, the below code was working fine .

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

但是现在"parent.opener" is returning null.因此,我能够关闭弹出窗口,但无法将父窗口重定向到地理位置.

But now "parent.opener" is returning null. So I am able to close the popup but not able to redirect the parent window todisired location.

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

I know what I am asking is bit wired, But this the requirement.

推荐答案

在"abc.moc"处

At "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"处

at "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

这篇关于window.opener正在重用null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:24