[英]If else statement not working in javascript onclick() when called from android webview?
在onClick()
函數中,其中有一個if else語句,它在android webview中無法正常工作。 我在下面添加了代碼。 如果我錯過了,請提供您的建議。 非常感謝。
function exampleCall(message) {
if(navigator.userAgent.match(/Android/i)) {
androidexampleCall(message);
}
if(navigator.userAgent.match(/iPhone|iPad|iPod/i)){
iOSexampleCall(message);
}
}
JS電話:
<div onclick="javascript:exampleCall('GameOn');">.... </div>
我想無效的通話是androidexampleCall(message)
。
定義一個界面並將其添加到您的活動的webview中,例如:
Webview wv;
//...
wv.addJavascriptInterface(new myJavaScriptInterface(), "CallToAnAndroidFunction");
//...
public class myJavaScriptInterface {
@JavascriptInterface
public void androidexampleCall(String message);{
//code
}
}
在您的Js代碼上,您應該按如下方式進行調用:
function exampleCall(message) {
if(navigator.userAgent.match(/Android/i)) {
window.CallToAnAndroidFunction.androidexampleCall(message);
}
if(navigator.userAgent.match(/iPhone|iPad|iPod/i)){
iOSexampleCall(message);
}
}
好像你沒有為你的Webview啟用Javascript:
webview.getSettings().setJavaScriptEnabled(true);
編輯:
使用此Webview測試您的代碼:
webview = (WebView)findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://jonathanalbrieux.com/others/testagent.html");
Button buttonReload = (Button)findViewById(R.id.button1);
buttonReload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
webview.loadUrl("http://jonathanalbrieux.com/others/testagent.html");
}
});
而這個頁面:
<html>
<head>
</head>
<body>
<div id="clickable" onclick="test('a')">clickaqd</div>
<script>
function test(string){
if(navigator.userAgent.match(/Android/i)) {
var divClickable = document.getElementById("clickable");
divClickable.innerHTML = string;
}
}
</script>
</body>
一切正常,也許你在androidexampleCall
函數中有一些問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.