'Load function from JavaScript file hosted on URL using script tag

I want to call function in WebView from URL based another JavaScript file. i.e

<script src="https://...Build/ChooseYourOwnAdventure.framework.js" ></script>

My WebView Code is:

<WebView
        source={{
          html: `<!DOCTYPE html>
            <html>  
            <body>
             <script src="https://.../Build/ChooseYourOwnAdventure.loader.js"></script>
                      
    <div id="unity-container">
    <canvas id="unity-canvas"></canvas>
    <div id="unity-loading-bar">
      <div id="unity-logo"></div>
      <div id="unity-progress-bar-empty">
        <div id="unity-progress-bar-full"></div>
      </div>
    </div>
  </div>

            <script>
      var buildUrl = "https://.../Build";
      var loaderUrl = buildUrl + "/ChooseYourOwnAdventure.loader.js";
      var config = {
        dataUrl: buildUrl + "/ChooseYourOwnAdventure.data",
        frameworkUrl: buildUrl + "/ChooseYourOwnAdventure.framework.js",
        codeUrl: buildUrl + "/ChooseYourOwnAdventure.wasm",
        streamingAssetsUrl: "StreamingAssets",
        companyName: "DefaultCompany",
        productName: "ChooseYourOwnAdventure",
        productVersion: "0.1",
      };

      var container = document.querySelector("#unity-container");
      var canvas = document.querySelector("#unity-canvas");
      var loadingBar = document.querySelector("#unity-loading-bar");
      var progressBarFull = document.querySelector("#unity-progress-bar-full");

      loadingBar.style.display = "block";

      var script = document.createElement("script");
      //var embedder = script[script.length - 1];

      script.src = loaderUrl;

      script.onload = () => {
         createUnityInstance(canvas, config, (progress) => {
           progressBarFull.style.width = 100 * progress + "%";
         }).then((unityInstance) => {
           loadingBar.style.display = "none";
         });
      };
       document.body.appendChild(script);
      //embedder.parentNode.appendChild(script);
</script>
 

            </body>

            </html>`,
        }}
      />

Here, createUnityInstance() is written in src="https://...Build/ChooseYourOwnAdventure.framework.js". I am trying to call it here with my custom config.

But, t shows undefined.

When I alert hello in onLoad() it works perfect.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source