Webview:postmessage(msg, [isjson]) method


Post a message to the current top level document rendered in the Webview.
To retrieve the message in the document, you must subscribe to the "message" event of the window.chrome.webview object from the top level document.

Parameters

msg

A valid string to be posted to the toplevel document of the Webview.

[isjson]

A boolean value indicating if the provided string is encoded as JSON, defaulting to false

Return value

This function returns true if the operation was successful or false otherwise.

Example

local html = [[ <!DOCTYPE html> <html> <head> <script> "use strict"; window.chrome.webview.addEventListener('message', arg => { if ("text" in arg.data) document.getElementById("text").innerHTML = arg.data.text; }); </script> </head> <body> <center><h1 id="text" style="margin-top:30%"></h1></center> </body> </html>]] local ui = require "ui" require "webview" -- create a simple Webview local win = ui.Window("Webview:postmessage() sample", 640, 480) local Webview = ui.Webview(win, "", 0, 50, 640, 430) local button = ui.Button(win, "Post message to Webview") button:center() button.y = 10 -- once ready, load the html content from string function Webview:onReady() Webview:loadstring(html) end function button:onClick() -- post a JSON message to the Webview Webview:postmessage('{ "text" : "Hello from Luart" }', true) end -- update the user interface until the user closes the Window ui.run(win):wait()