当H5 js方法没有定义时,需要做一些特殊的处理,我们可以这样做,来判断js方法是否存在.
比如判断webview加载url returnBackHandles Js 方法是否存在
private String js = "function androidBack(){if(typeof returnBackHandles != \"undefined\" && typeof returnBackHandles == \"function\")" +
"{console.log(\"returnBackHandle\");" +
"returnBackHandleNew();" +
"}else{" +
"if(typeof nativeBridge == \"undefined\") " +
"return false;" +
"var toNativeData = {" +
"methodName: \"nativeBridgeCallback['goBackCallback']\",errorMethodName: \"nativeBridgeCallback['goBackErrorCallback']\"};" +
"console.log(toNativeData);" +
"toNativeData = JSON.stringify(toNativeData);" +
"nativeBridge['goBack'](toNativeData);}}";
returnBackHandle 为判断的js方法
webView 调用
try {
webView.loadUrl("javascript:" + js + ";androidBack()");
} catch (Exception e) {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
}
网友评论