小编gir*_*rly的帖子

Android WebView并执行javascript

我有一个看起来像这样的html文件

<html>
<head>
     This is a test
    <script>
    function testingConsole() {
    console.log("Reached end of Body");
    }
    </script>


</head>

<body>
    <script type="text/javascript">testingConsole();</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像这样

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chart);

    webView = (WebView) findViewById(R.id.wvChart);
    webView.loadUrl("file:///android_asset/Chart/chart.html");
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());

    webView.loadUrl("javascript:testingConsole();");
}
Run Code Online (Sandbox Code Playgroud)

html文件正常加载,文本"This is a test"显示在我的WebView中.和脚本testingConsole(); 由于html文件正在调用它,因此应该自行执行.但是当我从我的代码中调用它时,我收到错误"Uncaught ReferenceError:testingConsole未定义为null:1"

javascript android android-webview

0
推荐指数
1
解决办法
3920
查看次数

标签 统计

android ×1

android-webview ×1

javascript ×1