5

I couldn't find any good answers on stack - they related to dialog boxes, text editors, and one guy used VBScript.

I need to post a message to the window opener. This works fine in FF, Chrome and Opera, but window.opener is null in IE8-10.

I am using window.open to make the new window appear.

I even tried this:

var new_window = window.open( url, '_social', "height=600,width=600" );

if ( !new_window.opener ) {
  new_window.opener = window;
}

The opened window simply has a script tag like this:

<script type="text/javascript">
var data = {
  type : 'redirect',
  destination : '<?= $destination; ?>'
};
window.opener.postMessage( JSON.stringify( data ), '*' );
window.close();
</script>

I opened the console and logged window.opener which comes up null, so I don't think it has anything to do with the DOM being ready or not.

The window does redirect a few times before landing on the page with the script tag.

Dave Stein
  • 8,653
  • 13
  • 56
  • 104

2 Answers2

1

Actually, the problem may not have anything to do with window.opener – because IE8+ can only use postMessage to communciate with an iframe.

http://blogs.msdn.com/b/ieinternals/archive/2009/09/16/bugs-in-ie8-support-for-html5-postmessage-sessionstorage-and-localstorage.aspx

Ben Vinegar
  • 307
  • 1
  • 8
  • "Unfortunately, this workaround often isn't possible, because same-origin-policy dictates that the popup window and the window.opener page must be from the same origin in order to call each other's script functions." So it appears that since I'm opening the window at a different domain, opener isn't there. – Dave Stein Sep 04 '13 at 22:22
  • Well, yeah, if their postMessage implementation was just a hack around same-origin window access – which it seems like it was. Worth noting that if you're trying to access windows on the same subdomain, at least, you can use document.domain trickery to obtain access. – Ben Vinegar Sep 04 '13 at 22:23
  • I actually have an iframe to my own domain, which opens a window to another domain - but the logic holds. If I use `window.open` to my own domain, `opener` is there. Long story why this craziness is going on. – Dave Stein Sep 04 '13 at 22:25
0

IE8 doens't like spaces. Remove them in order for it to work:

var new_window = window.open(url,'_social',"height=600,width=600");

if (!new_window.opener) {
  new_window.opener = window;
}
Joren
  • 3,068
  • 25
  • 44
  • This does not work in IE9 or 10 either. Also I've never had an issue with spaces between arguments anywhere. – Dave Stein Sep 04 '13 at 22:07