You are here: Reference > JavaScript > client-side > event handling > methods > postMessage (window)

postMessage method (window)

Browser support:
Provides communication between two documents regardless of their location.
Note: The postMessage method is supported in Internet Explorer from version 8, Firefox from version 3 and Opera from version 9.5.
Because of security restrictions, the contents of a document cannot be accessed from another document in JavaScript if the two documents are located in different domains. If you need to communicate between two documents regardless of their location, invoke the postMessage method on the window object that contains the target document. When the postMessage method is invoked, then an onmessage event will be fired on the target window.
Note: The postMessage method is synchronous in Internet Explorer and asynchronous in other browsers.
The event object that belongs to the onmessage event supports the following additional properties:
  • the data property contains the message,
  • the origin property retrieves the scheme, hostname and port of the document that invoked the postMessage method,
  • the source property refers to the window object that contains the document that invoked the postMessage method.
  • the lastEventId property returns the identifier of the last message.
Note: The origin property is supported in Opera from version 10. In earlier versions, it supports the uri and domain properties instead. In Opera 10, the support for the uri and domain properties has been removed.
See the example for further details.


object.postMessage (message, targetOrigin);
You can find the related objects in the Supported by objects section below.


Required. String that specifies the message.
Required in Firefox, Google Chrome and Safari, optional in Internet Explorer and Opera. String that specifies the URI (scheme, hostname and port) of the target document's location.
Before the message is dispatched, the current location of the target document is checked. If it does not match the specified URI, then the message will not be dispatched. This parameter can be useful if you want to be sure of the location of the target document before dispatching the message (for example secret data needs to be sent).
If you do not need this feature, use the '*' string literal.
Opera does not support this parameter.

Return value:

This method has no return value.

Example HTML code 1:

This example illustrates the use of the postMessage method:
    <script type="text/javascript">
        function Init () {
            if (window.addEventListener) {  // all browsers except IE before version 9
                window.addEventListener ("message", OnMessage, false);
            else {
                if (window.attachEvent) {   // IE before version 9
                    window.attachEvent("onmessage", OnMessage);

        function GetState () {
            var frame = document.getElementById ("myFrame");
                // send the 'getstate' message to the frame window
            var message = "getstate";
            if (frame.contentWindow.postMessage) {
                frame.contentWindow.postMessage (message, "*");
            else {
                alert ("Your browser does not support the postMessage method!");

        function OnMessage (event) {
            var message =;
            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.");
<body onload="Init ();">
    <iframe id="myFrame" src="message.htm" width="500" height="200px"></iframe>
    <br /><br />
    <button onclick="GetState ()">Get the state of the controls in the frame</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments
window.postMessage is syncronouse in IE 8
postMessage is syncronouse in IE 8

var syncronouse = true;
window.onmessage = function () {
  alert(syncronouse);// alerts "true"
window.postMessage('test', '*');
syncronouse = false;

Post Content

Post Content