Web Messaging

Web Messaging, or cross-document messaging, is an API introduced in the WHATWG HTML5 draft specification, allowing documents to communicate with one another across different origins, or source domains while rendered in a web browser. Prior to HTML5, web browsers disallowed cross-site scripting, to protect against security attacks. This practice barred communication between non-hostile pages as well, making document interaction of any kind difficult. Cross-document messaging allows scripts to interact across these boundaries, while providing a rudimentary level of security.

Requirements and attributes
Using the Messaging API's  method, plain text messages can be sent from one domain to another, e.g. from a parent document to an IFRAME. This requires that the author first obtain the  object of the receiving document. As a result, messages can be posted to the following: The message  being received has the following  attributes:
 * other frames or iframes within the sender document's window
 * windows the sender document explicitly opens through JavaScript calls
 * the parent window of the sender document
 * the window which opened the sender document
 * – The data, or actual content, of the incoming message.
 * – The origin of the sender document. This typically includes the scheme, hostname and port. It does not include the path or fragment identifier.
 * – the  of where the document came from (the source window).

is not a blocking call; messages are processed asynchronously.

Example
Consider we want document A loaded from  to communicate with document B loaded from   into an   or popup window. The JavaScript for document A will look as follows: The origin of our  object is passed to. It must match the  of the document we wish to communicate with (in this case, document B). Otherwise, a security error will be thrown and the script will stop. The JavaScript for document B will look as follows: An event listener is set up to receive messages from document A. Using the  property, it then checks that the domain of the sender is the expected domain. Document B then looks at the message, either displaying it to the user, or responding in turn with a message of its own for document A.

Security
Poor origin checking can pose a risk for applications which employ cross-document messaging. To safeguard against malicious code from foreign domains, authors should check the  attribute to ensure messages are accepted from domains they expect to receive messages from. The format of incoming data should also be checked that it matches the expected format.

Support
Support for cross-document messaging exists in current versions of Internet Explorer, Mozilla Firefox, Safari, Google Chrome, Opera, Opera Mini, Opera Mobile, and Android web browser. Support for the API exists in the Trident, Gecko, WebKit and Presto layout engines.