0

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.

user3714162
  • 89
  • 2
  • 7
  • `def.com` is a different domain. May require using `postMessage()` or other cross domain messaging method. You should be able to use `document.referrer` at new `window` to reference the url which opened new `window` – guest271314 May 10 '16 at 01:48
  • @guest271314 Yes 'dcoument.referrer' giving me the url that opened this new window. But how should I redirect in that window? – user3714162 May 10 '16 at 02:08
  • See https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage – guest271314 May 10 '16 at 02:19
  • @guest271314: `window.opener` itself returning null in my case because of cross domain, then how `window.open.postMessage` will work on the popup? – user3714162 May 10 '16 at 03:07
  • If you can add ` – guest271314 May 10 '16 at 04:00
  • @guest271314 : `If you can add – user3714162 May 10 '16 at 04:43
  • Does "abc" open "def"? If `.postMessage()` does not return expected results, you can try using `localStorage` – guest271314 May 10 '16 at 04:44
  • @guest271314 Yes it does open the popup. In the plunker demo are you using two different domains as i explained? – user3714162 May 10 '16 at 04:49
  • Try `window.postMessage("redirect.html", document.referrer);` – guest271314 May 10 '16 at 04:58

1 Answers1

2

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

guest271314
  • 1
  • 15
  • 104
  • 177