# \[Solved\] Possible to run javascript when screen opens?

**URL:** <https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076>\
**Category:** Questions about Thunkable X\
**Tags:** webviewer, javascript\
**Created:** [October 8, 2022, 10:37pm UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076 "2022-10-08T22:37:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [October 8, 2022, 10:37pm UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076/1 "2022-10-08T22:37:23Z")

</div>

When I use these blocks…

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/c/a/cae3ed8188c2822b9be1976ae1944c056ca46949.png)

…this event is **not** triggered…

![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/a/8/a82f743e64074afa18715517672829e570aad357.png)

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

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/b/2b402ec3d49500a9d9e1652d67536c83abf5095d.png)

…then it triggers the Receives Message event.

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

---

<div class="post-metadata">

**Author:** ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)\
**Post date:** [October 9, 2022, 9:28am UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076/2 "2022-10-09T09:28:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [October 9, 2022, 3:21pm UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076/3 "2022-10-09T15:21:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sketch](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/sketch/32/109058_2.png) [@sketch](https://community.thunkable.com/u/sketch)\
**Post date:** [October 9, 2022, 5:04pm UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076/4 "2022-10-09T17:04:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [October 9, 2022, 9:35pm UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076/5 "2022-10-09T21:35:18Z")

</div>

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

> [@sketch](#):
>
> as soon as it’s loaded in the webviewer using the onload event for the window.

---

<div class="post-metadata">

**Author:** ![sketch](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/sketch/32/109058_2.png) [@sketch](https://community.thunkable.com/u/sketch)\
**Post date:** [October 10, 2022, 4:55am UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076/6 "2022-10-10T04:55:11Z")

</div>

> [@tatiang](#):
>
> I’m still unclear how to do this:

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [October 10, 2022, 6:36am UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076/7 "2022-10-10T06:36:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)\
**Post date:** [January 8, 2023, 6:36am UTC](https://community.thunkable.com/t/solved-possible-to-run-javascript-when-screen-opens/2075076/8 "2023-01-08T06:36:27Z")

</div>

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