twa-dev / SDK

npm package for TWA SDK
https://codesandbox.io/s/sdk-kj5961
MIT License
238 stars 21 forks source link

How to listen for onload event of the script? #4

Closed 2ico closed 1 year ago

2ico commented 1 year ago

I'd like to show a loading page while the script is loaded. How can it be done with this library?

ArthurStam commented 1 year ago

First of all you need to load telegram SDK as soon as possible. In other words, you need to separate module telegram-web-apps from the rest code base and put it on the top of your HTML. Check out the example in our Boilerplate repo.

When it's done, just call WebApp.ready() right after telegram-web-apps chunk execution:

<html>
  <head>
    <script src="telegram-web-apps.js"></script>
    <script>WebApp.ready()</script>
  </head>
  <body>
    <div>Loader</div>
    <script src="app.js"></script>
  </body>  
</html>