Uncaught ReferenceError, calling javascript function through android app ..?

I am trying to call the javascript function setGameName () through the activity of the Android web view, but it throws uncaught ReferenceError:setGameName not defined at :1.

My source code is as follows:

webView2.setWebChromeClient(new WebChromeClient());
webView2.getSettings().setLightTouchEnabled(true);
webView2.getSettings().setJavaScriptEnabled(true);
webView2.addJavascriptInterface(jsInterface, "AndroidFunction");
webView2.loadUrl("file:///android_asset/www/index.html");
webView2.loadUrl("javascript:setGameName()");

HTML code:

    <body>

        <script type="text/javascript">
            function init() {
                console.log("====== I am getting called::===");
                var testVal = document.getElementById('playertextId').value;
                AndroidFunction.proceedFurther(testVal);
            };

            function setGameName() {
                console.log("====== Got the value===");
                document.getElementById('gametextId').value;
            };
        </script>
</body>

It loads index.html perfectly, but throws an exception when calling a function. Please provide an appropriate solution.

Update:

i have found the problem the page is not getting loaded at the time of function call..(Since on giving a time delay of 5000ms it is getting called..)!!

But how to implement it then ??

+5
source share
2 answers

loadUrl is asynchronous. This means that it returns immediately before the page loads. Therefore, setGameName does not yet exist when you try to call it.

WebClient onPageFinished javascript .

+4

, , , :///android_asset/www/index.html -, . JS JS .

0

All Articles