Note: This documentation is preliminary and is subject to change.
Sends a cross-document message.
Syntax
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
.
Applies To
See Also
onmessage