2

I'm loading a local HTML file in my WebView and executing my JS-Function like so:

webview.evaluateJavascript("start();", null);

It works as long as I have the function declared in <script></script> tags in the HTML file.

    ...
    <body>

    <script type="text/javascript">

    function start(){

    }
    </script>

    </body>
    ...

But now I'm first using Webpack to bundle my Javascript code into a single JS file and store this in the assets folder of my app and load it like so:

<script type="text/javascript" src="file:///android_asset/js/bundle.js"></script>

The new bundle.js file contains the start function but now I can't call it, because it's not defined.

What I'm missing here?

EDIT: Ok my function is only accessible in the bundle.js. There are multiple ways to make it work, as I want:

Define global variable with webpack

I choose the following solution to make my function accessible in the global context:

window.start = function() {

}
BR75
  • 633
  • 7
  • 25
  • Do you wait for page fully loaded before script evaluation? – stck Jan 21 '20 at 15:17
  • yes...webview.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) {..... – BR75 Jan 21 '20 at 15:18
  • https://stackoverflow.com/questions/59632372/how-can-i-call-a-dialog-from-a-webview?answertab=active#tab-top may help. I did'n get your exact problem – Muneesh Jan 21 '20 at 17:19

0 Answers0