Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
HTML and CSS
Methods
 postMessage Method

  Switch on low bandwidth view
postMessage Method
New for Windows Internet Explorer 8

Sends a cross-document message.

Syntax

object.postMessage(msg [, targetOrigin])

Parameters

msg Required. A String that contains the message data.
targetOrigin Optional. A String that specifies a target Uniform Resource Identifier (URI).

Return Value

No return value.

Remarks

Note  "*" is a valid value for targetOrigin.

The postMessage method allows cooperative text exchange between untrusted modules from different domains embedded within a page. It does so by ensuring a consistent and secure process for text-based data exchange.

When a script invokes this method on a window object, the browser sends an onmessage event to the target document on which the data property has been set to the value passed in msg.

When a target URI is passed into the method, it must have at least a protocol and a host specified. The message will only be sent if the target window has the same protocol and host as the specified target URI.

The postMessage method call does not return until the event listeners of the target document have finished executing.

Examples

If Document A contains a reference to the contentWindow property of Document B, script in Document A can send a message to Document B by calling the postMessage method as follows:

var o = document.getElementsByTagName('iframe')[0];
o.contentWindow.postMessage('Hello World');

The script in Document B can respond to the message by registering the onmessage event handler for incoming messages.

window.attachEvent('onmessage',function(e) {
    if (e.domain == 'example.com') {
        if (e.data == 'Hello World') {
            e.source.postMessage('Hello');
        } else {
            alert(e.data);
        }
    }
});

Standards Information

This method is defined in HTML 5 World Wide Web link.

Applies To

window, Window Constructor

See Also

onmessage
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
deviation from the HTML 5 spec      Steve Souders ... EricLaw-MSFT   |   Edit   |   Show History

IE 8 Beta1 included this feature before the HTML5 proposal was changed. The IE8 Beta2 build will be updated to comply with the current HTML5 proposal.

This sample page demonstrates that IE8 Beta 1( I'm running 8.0.6001.17184 ) deviates significantly from the HTML5 spec for postMessage:

http://stevesouders.com/misc/test-postmessage.php

The specific issues:
  • The HTML 5 spec says, "If the value of the targetOrigin argument is neither a single U+002A ASTERISK character ("*") nor a valid URI or IRI, then throw a SYNTAX_ERR exception...". IE8 doesn't thrown an exception when the second parameter is missing.
  • The HTML 5 spec says, "If the value of the targetOrigin argument is neither a single U+002A ASTERISK character ("*") nor a valid URI or IRI, then throw a SYNTAX_ERR exception...". Even though "*" is valid according to the spec, IE8 still throws an exception and aborts.
  • The HTML 5 spec says, "When a script invokes the postMessage(message, targetOrigin) method on a Window object,...". But postMessage only works when it's attached to the document object in IE8.


  • Tags What's this?: Add a tag
    Flag as ContentBug
    thanks      mac111 ... Shadow Chaser   |   Edit   |   Show History
    (deleted spam)
    Warning about feature detection with IE6 and IE7      Shadow Chaser   |   Edit   |   Show History
    Don't use "typeof(window.postMessage)" to determine whether a browser supports the HTML5 postMessage feature - both Internet Explorer 6 and 7 return a value of "function" as the type!

    It appears as though IE6 and IE7 have some sort of undocumented function called postMessage that has a completely different purpose. It could cause conflicts if you're not careful when calling this function.
    Processing
    © 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
    Page view tracker