2016-12-26 47 views
0

我想使用WebView为Android创建一个Web应用程序。我cenary很简单,我有一个按钮,一个index.html文件:关于Android WebView的JS级别15的JS ReferenceError

<button id="connect-button" onclick="connect">Connect</button> 

和简单(脚本)元素:

<script> 
connect = function() { 
    alert("Oi"); 
}; 
</script> 

在Android方面我有一个在Webview上加载此文件的活动

WebView web; 
web = (WebView) findViewById(R.id.webview01); 

//ProgressBar related to the xml 
progressBar = (ProgressBar) findViewById(R.id.progressBar1); 
progressBar.setMax(100); 

web.setWebViewClient(new myWebClient()); 
web.setWebChromeClient(new MyWebViewClient()); 
web.getSettings().setJavaScriptEnabled(true); 
web.getSettings().setLoadWithOverviewMode(true); 
web.getSettings().setUseWideViewPort(true); 
web.loadUrl("file:///android_asset/index.html"); 

对于api级别19或以上的一切正常。但对于API 15到18级的webview无法加载我简单的JavaScript方法“连接”。

我搜索了很多关于webview for api level bellow 19的问题,但这是非常简单的测试。有时某人会做出类似于api 15的事情?

回答

0

解决

对于我来说,问题是,aparently的WebView原料药比19下,你必须把所有的代码明确的“窗口”对象。

所以,当我改变我的Java脚本

<script> 
window.connect = function() { 
    window.alert("Oi"); 
}; 
</script> 

一切正常!

我们有很多的相似对这个问题的问题,如果这对你也检查不工作: