-4

I have some javascript that relies on the use of window.open and inter-window communication (where the new 'child' window communicates success / failure to the 'parent' window).

This (inter-window communication) works fine in most cases, but doesn't work for some browsers that don't support inter-window communication. Examples include Windows Phone and iPhone's UIWebView.

I currently do client-side (javascript based) user-agent sniffing to detect these scenarios - and fallback to a different code path to workaround the issue.

Is there an alternative to user-agent sniffing that I could use?

To be clear, the window.open works (where works means 'it opens the requested url'). What isn't reliable is using window.opener and postMessage to do inter-window communication from the 'child' to the 'parent'.

Community
  • 1
  • 1
mjwills
  • 23,389
  • 6
  • 40
  • 63
  • The child can also access the window.opener property - which should be null - but unfortunately this isn't a reliable detection technique. As an example, if you open the child window directly (without using window.open from the parent) then window.opener will be null even though the child actually does actually support inter-window communication. – mjwills Feb 25 '13 at 06:43
  • i know `window.open` will work, but will the function returns child window ID ? – Raptor Feb 25 '13 at 06:49
  • I am not sure how having that information will help. What I want is feature detection inside the child window as to whether it can talk to its parent. The return value of the window.open function call is obviously available only to the parent - so 'it rather involved being on the other side of this airtight hatchway'. All that being said, checking the return value yields null in mobile IE9, and a window object in UIWebView. And note that in both cases the new window was loaded correctly (but will be unable to communicate with its parent). – mjwills Feb 25 '13 at 08:51
  • What have you tried for detection? What errors do you get when omitting the test? Or does everything execute as normal, only with no message sent? – Bergi Feb 25 '13 at 12:49
  • You could have a better cross-browser support with BNC Connector: http://theprivateland.com/bncconnector/index.htm . Detection is pretty simple I think, you try to communicate with the child window, if it fails, then it does not work... :-) – inf3rno Oct 03 '13 at 23:16

1 Answers1

3

In your opener window message callback you may send a reply back to the child window like this:

function yourMessageCallback(event) {
  // your other handler stuff here...
  event.source.postMessage('Yeah I got it', event.origin);
}

Then you can do a timer on the sending side that you can clear when the reply arrives:

// do your postmessage here

function notReceived() {
  // do stuff if the message didn't go through
}

var messageTimer = setTimeout(notReceived, 500); // 500ms should be enough for everyone?

window.addEventListener('message', function(event) {
  // do necessary origin checks first etc... (not shown here)

  if (event.data == 'Yeah I got it') {
    clearTimeout(messageTimer);
    // do stuff if the message went through
  }
}, false);

I know this might be a bit hackish solution but maybe less so than user agent sniffing?

sactor
  • 278
  • 3
  • 7
  • I fear I may be misunderstanding your proposal @sactor. Is the first code block to be placed inside the parent window and the second to be placed inside the child window? Keep in mind that [postMessage to the parent won't work](http://stackoverflow.com/a/12265816/34092). – mjwills Feb 25 '13 at 22:46
  • Or are you suggesting 'just try postMessage, and if we don't get an 'ack' back then we know it didn't work'.? That could work but likely has the same issue as the 2nd comment on my original post. So if the child window is opened directly by the end user in a browser - rather than by the parent's window.open - then your proposed technique won't be able to detect whether inter-frame communication is supported or not. – mjwills Feb 25 '13 at 22:56
  • Yes I'm just suggesting to check if we get ack back. I thought that your aim was to check if you can actually get the message to the parent, not if it's theoretically possible (if it wasn't opened the wrong way). For that I don't unfortunately have an answer. – sactor Feb 26 '13 at 09:16