user3714162
user3714162

Reputation: 89

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 . 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 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 .

<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. 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.

Upvotes: 0

Views: 6815

Answers (1)

guest271314
guest271314

Reputation: 1

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>

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

Upvotes: 2

Related Questions