Message Communication Between Host and Embedded Web Pages

When you embed analytic application in a host HTML page or embed a web page in analytic application through the web page widget, you can follow this guide to enable message communication between host and embedded web pages.

Using the posting message API, you as the application developer can realize either of the following scenarios:

Scenario 1: Embedding analytic application in a host HTML page via iFrame

Before embedding an analytic application via a iFrame in the host HTML page, you need to first make sure the host HTML page is added as a trust origin in the Start of the navigation pathSystem Next navigation step Administration Next navigation step App Integration Next navigation step Trusted OriginEnd of the navigation path.

Then you can trigger bi-direction communication between the host HTML page and analytic application using the provided functions.

postMessage

This is to post messages from the analytic application to the host HTML page.

When an end user triggers a callback function in the analytic application side, the callback function sends out data to notify the parent receiver page which hosts the iFrame, or, when there are multiple levels of web pages embedded in one another, to the top-level HTML page of a specific target origin.

You define whether to send data to a parent or the top HTML page by means of the parameter of the PostMessageReceiver.

The syntax of the postMessage event is:

postMessage (receiver: PostMessageReceiver, message: string, targetOrigin: string): void

onPostMessageReceived

This is to handle messages sent from the host parent or top HTML page in the analytic application. In scenario 2 below, the event can also handle messages sent from an HTML page embedded via the web page widget in an analytic application.

Caution

We advise you always check the origin when receiving an event-triggered message, because a malicious site can change the location of the window and therefore intercept the data you sent using the postMessage event without your knowledge.

In the current scenario, the parent window which hosts the iFrame can post messages to the analytic application's iFrame window of specific target origin. The messages posted are then retrieved by the analytic application and trigger changes accordingly, such as updating some input data.

The syntax of the onPostMessageReceived event is:

onPostMessageReceived(message: string, origin: string)

A Code Example

You embed an analytic application in a host HTML page. The URL of the host HTML page is http://localhost:8080.

First, you want to allow end users to post the company selection in the analytic application to the host HTML page. Write the script below for the sending button:

Sample Code
var message = RadioButtonGroup_Company.getSelectedText();
Application.postMessage(PostMessageReceiver.Parent, message, "http://localhost:8080");

Then you want to allow end users to display the message received from the Host HTML page in a text box of the embedded analytic application. Click the button beside Main Canvas, choose onPostMessageReceived and write the script below for the application:

Sample Code
If (origin == “http://localhost:8080”) {
Text_ReceivedMessage.applyText(message);}
Scenario 2: Embedding web application in an analytic application through the web page widget

You can trigger bi-direction communication between the embedded web application and the host analytic application.

Web Page Widget Related postMessage and OnPostMessageReceived

When the host analytic application's web page widget embeds a web application, you can post messages from the embedded application to the host analytic application or the other way around.

The syntax of the postMessage event is:

postMessage(message: string, targetOrigin: string): void
Note
The target origin is optional. If left empty, the URL defined in the web page widget will be taken as the target origin by default.

The syntax of the onPostMessageReceived event is:

onPostMessageReceived(message: string, origin: string)

Case 1. Posting messages from the host analytic application to the embedded application

The event for posting messages is:

postMessage()

The event for handling messages sent from the host analytic application depends on the type of the embedded application:

  • If the embedded application is an SAP Analytics Cloud application, once the message is received, the embedded application can use the event onPostMessageReceived() to handle the message.
  • If the embedded application is another web application, once the message is received, the embedded application can use the event window.on("message") to handle the message.

Case 2. Posting messages from the embedded application to the host analytic application

The event for posting messages depends on the type of the embedded application:

  • If the embedded application is an SAP Analytics Cloud application, use the event Application.postMessage() to post messages.
  • If the embedded application is another web application, use the event window.parent.postMessage to post messages.

The event for handling messages sent from embedded application is:

Once the messages is received, the host application can use the event onPostMessageReceived() to handle the messages.