You are here: Reference > JavaScript > client-side > event handling > methods > initMessageEvent (event)
initMessageEvent method (event)
9 | 3 | 10.6 | ||
Initializes an event object created by the createEvent method with type of 'MessageEvent'.
Note: The initMessageEvent method is supported in Firefox from version 3, Opera from version 10.6 and Internet Explorer from version 9.
An event object created by the createEvent method with type of 'MessageEvent' implements the MessageEvent interface.
After initialization, the event can be dispatched with the dispatchEvent method.
The postMessage method can also be used to fire an event with type of 'MessageEvent'.
In Internet Explorer before version 9, use the createEventObject method to create a new event object and the fireEvent method to dispatch it.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the name (type) of the event to initialize.
This parameter is case sensitive! Sets the type property of the event object.
For a complete list of events, see the page for Events in JavaScript.
The initMessageEvent method is appropriate for initialization of MessageEvent types principally, for other events, use the type-specific initialization methods.
Note: custom event names are also allowed.
Events represented by the MessageEvent interface: |
|||||||||||||||||||||||
Required. Boolean that specifies whether the event can bubble or not. Sets the bubbles property of the event object.
One of the following values:
|
|||||||||||||||||||||||
Required. Boolean that specifies whether the event can be canceled or not. Sets the cancelable property of the event object.
One of the following values:
|
|||||||||||||||||||||||
Required. String that specifies the message for the event. Sets the data property of the event object. | |||||||||||||||||||||||
Required. String that specifies the scheme, hostname and port of the document that caused the event. Sets the origin property of the event object. | |||||||||||||||||||||||
Required. String that specifies the identifier of the last event. Sets the lastEventId property of the event object. | |||||||||||||||||||||||
Required. Refers to the window object that contains the document that caused the event. Sets the source property of the event object. | |||||||||||||||||||||||
Required in Opera, optional in Safari and Google Chrome, not supported and omitted in Firefox and Internet Explorer. Specifies the message ports. Default is null. |
Return value:
This method has no return value.
Example HTML code 1:
This example illustrates the use of the initMessageEvent method:
|
|||||
<head> <script type="text/javascript"> function Init () { if (window.addEventListener) { window.addEventListener ("message", OnMessage, false); } } function GetState () { if (document.createEvent) { var frame = document.getElementById ("myFrame"); // send the 'getstate' message to the frame window // use the dispatchEvent instead of the postMessage method var messageEvent = null; try { messageEvent = document.createEvent('MessageEvent'); } catch (e) { alert ("Your browser does not support the createEvent method for the MessageEvent event type!"); return; } var message = "getstate"; var origin = window.location.protocol + "//" + window.location.host; var lastEventId = 12; if (messageEvent.initMessageEvent) { messageEvent.initMessageEvent ("message", true, true, message, origin, lastEventId, window, null); frame.contentWindow.dispatchEvent (messageEvent); } else { alert ("Your browser does not support the initMessageEvent method!"); } } else { alert ("Your browser does not support the createEvent method!"); } } function OnMessage (event) { var message = event.data; var arr = message.split (","); if (arr[0] == "true") { alert ("The check box is checked."); } else { alert ("The check box is not checked."); } var selIndex = Number (arr[1]); alert ("The " + (selIndex + 1) + ". option is selected."); } </script> </head> <body onload="Init ();"> <iframe id="myFrame" src="message.htm" width="500" height="200px"></iframe> <br /><br /> <button onclick="GetState ()">Get the state of controls in the frame</button> </body> |
|||||
|
|||||
Did you find this example helpful?
|
Supported by objects:
Related pages:
event
createEvent
initEvent
initDragEvent
initKeyEvent
initMouseEvent
initMutationEvent
initOverflowEvent
initTextEvent
initUIEvent
createEvent
initEvent
initDragEvent
initKeyEvent
initMouseEvent
initMutationEvent
initOverflowEvent
initTextEvent
initUIEvent
External links:
User Contributed Comments