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

onmessage Event
New for Windows Internet Explorer 8
Note: This documentation is preliminary and is subject to change.

Fires when the user sends a cross-document message with postMessage.

Syntax

Event propertyobject.onmessage = handlerJScript 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

BubblesNo
CancelsYes
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 World Wide Web link.

Applies To

document, HTMLDocument Constructor

Azure Services Platform
Design Tools
Development Tools and Languages
Mobile and Embedded Development
.NET Development
Office Development
Open Specifications
Servers and Enterprise Development
Web Development
Win32 and COM Development

MSDN Library
Tags: 
 
Community Content