Skip to content

Script Examples

Transmit data from Webpage to UI Builder WebPage Component

In this example, we can transmit data from the UI Builder WebPage component to the Web Page iframe element as well as transmit data from the Web Page iframe element to the UI Builder WebPage component.

Setting it up

  1. Save the iframe.html listing below somewhere your browser can reach it, and note its URL.
  2. Add a Web Page component to your user interface, name it webPageComponent and set its URL property to that address. The same works for a Web App component: both support onMessage and postMessage.
  3. Add a text input named message (what you send), a text input named receive (what you get back) and a button named sendBtn.
  4. Paste the script below.

UI Builder Script(WebPage.ezui)

1
2
3
4
5
6
7
webPageComponent.onMessage = e => {
  receive.value = e.data;
}

sendBtn.onClick = () => {
  webPageComponent.postMessage(message.value);
}

Tip

The event also carries e.origin, the origin of the window that sent the message. Check it before trusting e.data if the page can be loaded from more than one source.

Warning

postMessage is forwarded straight to the embedded page. If the page has not finished loading there is no window to deliver to and the message is dropped without an error, so send only after the page is up - for example from a button, as above, rather than at script start.

iframe.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<html>

<head>
</head>

<body>
  <p>
    Send to UIBuilder:<br />
    <textarea id="message" rows="5" cols="50">Hello from iframe!</textarea>
    <br />
    <button type="button" onclick="send()">Send</button>
  </p>

  <br />

  <p>
    Received from UIBuilder:<br />
    <textarea id="receive" rows="5" cols="50" readonly>
  </textarea>
  </p>


  <script>
    function send() {
      const message = document.getElementById('message').value;
      window.parent.postMessage(message, '*');
    }

    window.onmessage = function(e) {
      const receive = document.getElementById('receive');
      receive.value = e.data;
    }
  </script>
</body>

</html>