www.fgks.org   »   [go: up one dir, main page]

Click to Rate and Give Feedback
Methods
 postMessage Method
postMessage Method
New for Windows Internet Explorer 8
Note: This documentation is preliminary and is subject to change.

Sends a cross-document message.

Syntax

window.postMessage(msg)

Parameters

msg Required. A String that contains the message data.

Return Value

No return value.

Remarks

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.

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.

document.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

See Also

onmessage
Tags What's this?: Add a tag
Community Content
 
Add Community Content
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker
DCSIMG