Message Property  
 

Returns the most recent message sent from the current document.

Syntax

object.Message

Remarks

The Message property returns the most recent message sent from a script running in the browser. This is typically used to enable interaction between the web content displayed in the browser and the host application. For example, a page might notify the application when a user completes a form, clicks a button, or when some script logic produces a result that the native application needs to act upon. This provides a simple and secure channel for exchanging data between the web page and the host, without requiring the page to reload or navigate to a new URL.

Messages are sent from JavaScript using the following method:

window.chrome.webview.postMessage(value);

The format of the message depends on the type of value that was posted:

  • If the message was a structured JavaScript object or array, it will be returned as a JSON object string using a format like {"key":"value"}.
  • If the message was a string, it will be returned as a JSON-formatted string with surrounding quotes and any special characters will be escaped, such as "Hello\nWorld".

Applications that need to process these messages should parse the result as JSON to properly distinguish between plain strings and structured data.

It is not required for you to check this property inside a handler for the MessageReceived event. The message text returned by this property remains available until the next message arrives. If you need to preserve the contents of the message outside of the context of the current browser session, your application should always store its own copy. The control does not maintain a history of messages received.

Applications that receive messages from web content should consider validating the source of each message before processing it, particularly if the message can trigger privileged operations or access sensitive application data. The MessageSource property can be used to retrieve the URI of the document that sent the message, allowing the application to verify that it originated from a trusted source. Applications should not assume that messages are trustworthy simply because they were received by the browser. When validating the source, compare the complete origin, including the scheme, hostname and port, against a list of trusted origins rather than relying on partial string comparisons. Messages from unknown or untrusted sources should be rejected.

Important: Message passing is disabled by default. To enable this functionality, the AllowMessages property must be set to True; otherwise, no messages will be received and this property will not return any content.

The control's message handler will only receive messages posted directly by the top-level document, not by child frames. Note that there are security considerations with allowing messages from cross-origin iframes. The browser's same-origin policy doesn't prevent an iframe from sending messages through WebView2's native messaging interface and this can have unintended consequences.

There is an internal limit of 1,048,576 characters as the maximum size of a message.

Data Type

String

See Also