Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

top.postMessage origin error not caught

I'm trying to implement communication with postMessage. There is the main page which opens a popup with an iframe which comes from a different domain. This works fine so far but I want to catch the following error which occurs when I open the iFrame with a wrong origin.

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('myOriginURL') does not match the recipient window's origin ('myWindowsOrigin').

origin = 'http://www.myorigin.ch';
if (window.postMessage) {
  try {
     top.postMessage('hello', origin);
  } 
  catch(ex) {
     alert('an error occured');
  }
}

the problem is that the code never runs into the catch block. Interesting part is that chrome shows an error in the console while all other major browser just don't do anything (no alert, no error)

How can I handle the error in the postMessage?

like image 435
Arikael Avatar asked May 27 '14 07:05

Arikael


People also ask

Can postMessage be intercepted?

A malicious site can change the location of the window without your knowledge, and therefore it can intercept the data sent using postMessage .

Do iframes have their own window?

In action: iframeAn <iframe> tag hosts a separate embedded window, with its own separate document and window objects. We can access them using properties: iframe.

What does postMessage return?

postMessage() sends a message back to the main page. The two ends can listen to messages from the other using window.

Is postMessage synchronous?

The postMessage() function is asynchronous, meaning it will return immediately. So you can not do synchronous communication with it. In your example, the posted message will vanish in the void, because there is no listener for the message event at the time the postMessage() function is executed.


1 Answers

I think this is made on purpose. Your context should not know if the message was sent successfully, or not (for security purposes)

like image 104
licancabur Avatar answered Sep 20 '22 06:09

licancabur