sendMessage()

The sendMessage() function sends messages from the current window to the parent window. You can combine addMessageListener() and sendMessage() to enable communication between different browser windows. For more information, see addMessageListener().

Syntax

Use these syntax formats for the sendMessage() function:

  • To send a general message:

                    sendMessage(dataObject); 
    
                  
  • To send a specific message type:

                    sendMessage(messageType, dataObject); 
    
                  

Return Value

The sendMessage() function returns undefined.

Parameters

Note:

The data parameter is required.

The sendMessage() function accepts the following parameters:

  • messageType (string) - Specifies the type of message to send.

  • dataObject (object) - Specifies the data to send.

Examples

The following examples show how to use the sendMessage() function.

Sending a General Message

This example sends a message containing only the data object without specifying a message type.

              sendMessage({deskType: 'Office Desk'}); 

            

This is equivalent to:

              parentWindow.postMessage({deskType: 'Office Desk'},'*'); 

            

Sending a Specific Message Type

This example sends a message of the specific type and the provided data object. In this case, the massage type is get-options and the data object specifies the desk type. This can be used to request product options for a specific desk from the parent window.

              sendMessage('get-options', {
    deskType: 'Office Desk'
}); 

            

This is equivalent to:

              parentWindow.postMessage({type:'get-options', data: deskType: 'Office Desk'}},'*'); 

            

Related Topics

General Notices