未捕获的ReferenceError,通过android应用程序调用javascript函数..?

前端之家收集整理的这篇文章主要介绍了未捕获的ReferenceError,通过android应用程序调用javascript函数..?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图通过android Webview Activity调用一个javascript函数setGameName(),但它抛出未捕获的ReferenceError:setGameName未定义于:1.

我的源代码如下:

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代码

    

它正在加载index.html,但在函数调用时抛出异常,
请提供适当的解决方案..

更新:

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..)!!

但是我如何实施呢?

最佳答案
loadUrl是异步的.这意味着它会在页面加载之前立即返回.因此,当您尝试调用它时,setGameName尚不存在.

您可以使用onPageFinished方法实现WebClient并在那里调用您的javascript代码.

原文链接:https://www.f2er.com/js/429718.html

猜你在找的JavaScript相关文章