[Solved] Possible to run javascript when screen opens?

When I use these blocks…

…this event is not triggered…

image

But if I put the same Post Message block in a button click event…

…then it triggers the Receives Message event.

Is there a way that I can run javascript when the screen opens?

It’s a timing issue. The webviewer isn’t ready on screen open most of the time.

You can send a message from viewer to app when everything is loaded in the webviewer. That message then could trigger the app to post a message to the webviewer.

I did find another way to post a message from a button click so I’m okay for now but… if I wanted to send a message when the webviewer is loaded, how would I do that?

You could maybe use a wait() block and set it to 2 seconds or longer/shorter depending on the wait time for the webviewer to load.

Or you can make a postMessage in your web app as soon as it’s loaded in the webviewer using the onload event for the window.

Then you can use the message recieved event and place your blocks there.

1 Like

Thanks for the suggestions! I’m still unclear how to do this:

It’s simple place the following code on the first line in your js file.

window.onload = function() {
ThunkableWebviewerExtension.postMessage('loaded');
}

The onload event will fire you code as soon the page is loaded in the WebViewer.

Then you use the message recieved event and check Wich message is sent and then you run your blocks.

3 Likes

Thank you!!! That did the trick. I now have javascript running when the webviewer loads. In my app, a label’s text is set to today’s date when the screen opens.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.