最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

uncaught ReferenceError,Calling javascript function through android application..? - Stack Overflow

programmeradmin3浏览0评论

I am trying to call a javascript function setGameName() through android Webview Activity but it is throwing 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 is loading index.html fine but throwing exception at function call, 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 do I implement it then..?

I am trying to call a javascript function setGameName() through android Webview Activity but it is throwing 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 is loading index.html fine but throwing exception at function call, 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 do I implement it then..?

Share Improve this question edited Mar 11, 2013 at 10:09 Amritpal Singh asked Mar 11, 2013 at 9:33 Amritpal SinghAmritpal Singh 1,0023 gold badges15 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

loadUrl is asychronous. That means it returns immediately before the page has loaded. So setGameName does not yet exist when you try to call it.

You can implement a WebClient with an onPageFinished method and call your javascript code there.

I am not very sure but probably you need to put file:///android_asset/www/index.html on a webserver and then retrieve it. By default browsers don't let JS load from JS originating from Filesystems.

发布评论

评论列表(0)

  1. 暂无评论