android4.4 webview调用javascript出现Uncaught ReferenceError: is not define或者has no method
2014-10-18 16:16
423 查看
这几天用Nexus5 4.4.4系统做一个简单的手机设备号获取,然后调用javascript显示在网页里的功能,以前做过n多类似的程序,结果程序一运行啥问题都出来了,呵呵
[INFO:CONSOLE(1)] "Uncaught ReferenceError: is not define
I/chromium(490): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token ILLEG
[Android] Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'
总结来看就是上述问题,先看代码吧
demo.html
看看我的android 代码如下:
当loadUrl方法在activity的oncreate方法里,mWebview.loadUrl("file:///android_asset/demo.html"); 执行之后调用loadUrl();
基本上就出现上面的错误了,我想是不是4.4的系统在写法上跟低版本不一样,还是用了2种调用方式:
那最后的判断就是一种可能: 调用getDeviceID 方法的时候,js没有加载完毕。
解决办法:
我们在 onPageFinished的时候,全部的js肯定都加载完毕了,这时候再执行loadUrl方法,就可以啦。
最后顺便提一下 webview.evaluateJavascript的好处,这个方法,可以把js函数的return结果获取到:
加入CSDN技术群:221057495 与我交流
[INFO:CONSOLE(1)] "Uncaught ReferenceError: is not define
I/chromium(490): [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected token ILLEG
[Android] Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'
总结来看就是上述问题,先看代码吧
demo.html
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> </head> <script> function getDeviceID(info){ alert("设备号:"+info); //document.write("设备号:"+info); return "good."; } </script> <body> <a href="http://www.baidu.com" target="_blank"> href="http://www.baidu.com" 打开baidu </a> <br/> <br/> <a href="http://www.baidu.com/closeappwin" target="_blank"> href="http://www.baidu.com/closeappwin" 关闭浏览器closeappwin </a> <br/> <br/> <a href="new://www.sohu.com/" target="_blank"> href="new://www.sohu.com/" 在当前浏览器打开一个子浏览器new subwindow </a> <br/> <br/> </body> </html>
看看我的android 代码如下:
private void loadUrl(){ String key=""; String androidID=""; try{ androidID = Secure.getString(getContentResolver(),Secure.ANDROID_ID); Log.d(TAG, "androidID:"+androidID); }catch(Exception e){ Log.e(TAG, ""); }finally{ String script=String.format("javascript:getDeviceID('"+androidID+"')"); mWebview.evaluateJavascript(script, new ValueCallback<String>() { @Override public void onReceiveValue(String value) { Log.d(TAG, "onReceiveValue value=" + value); if(value!=null){ flag_get_deviceid=true; } }}); //mWebview.loadUrl("javascript:getDeviceID('maomao')"); } }
当loadUrl方法在activity的oncreate方法里,mWebview.loadUrl("file:///android_asset/demo.html"); 执行之后调用loadUrl();
基本上就出现上面的错误了,我想是不是4.4的系统在写法上跟低版本不一样,还是用了2种调用方式:
mWebview.evaluateJavascript
mWebview.loadUrl("javascript:getDeviceID('maomao')");
那最后的判断就是一种可能: 调用getDeviceID 方法的时候,js没有加载完毕。
解决办法:
private class WebViewClientDemo extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.d(TAG, " url:"+url); view.loadUrl(url);// 当打开新链接时,使用当前的 WebView,不会使用系统其他浏览器 return true; } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); //在这里执行你想调用的js函数 if(!flag_get_deviceid){ loadUrl(); } } }
我们在 onPageFinished的时候,全部的js肯定都加载完毕了,这时候再执行loadUrl方法,就可以啦。
最后顺便提一下 webview.evaluateJavascript的好处,这个方法,可以把js函数的return结果获取到:
mWebview.evaluateJavascript(script, new ValueCallback<String>() { @Override public void onReceiveValue(String value) { Log.d(TAG, "onReceiveValue value=" + value); if(value!=null){ flag_get_deviceid=true; } }});
加入CSDN技术群:221057495 与我交流
相关文章推荐
- Android JS桥交互("Uncaught ReferenceError: xxx is not defined or xxx has no method")
- Android webview js 调用java方法报错"Uncaught TypeError: Object [object Object] has no method xx
- android中java和JS互相调用,Web Console: Uncaught TypeError: Object [object Object] has no method
- Android webview刷新 Uncaught ReferenceError: isReadyForPullUp is not defined at null:1
- JS调用Android中的方法时出现 "Uncaught TypeError: Object [object Object] has no method
- ASP.NET在调用js方法时出现Uncaught ReferenceError:PageMethods is not defined的解决方法
- 安卓WebView报错:android uncaught typeerror object object object has no method
- 打开页面后调用方法$(function() {});在浏览器控制台出现Uncaught ReferenceError: $ is not defined
- 关于webview调用js出现has no method 'toString'
- (启动H5本地存储)WebView显示网页出现Uncaught TypeError: Cannot call method 'getItem' of null异常
- html-webpack-plugin2.22.0、Webpack与gulp结合时出现ReferenceError: window is not defined错误的解决办法
- javascript Uncaught ReferenceError: 方法名 is not defined
- 关于使用JQuery时 定义$(funtion(){});时出现Uncaught ReferenceError: $ is not defined的错误
- beatPicker使用出现Uncaught ReferenceError: module is not defined
- jQ中遇到的小问题-----Jquery调用出现ReferenceError: $ is not defined
- 关于webview调用js出现has no method 'toString'
- ThinkPHP ThinkAjax提交问题 ‘Uncaught ReferenceError: ThinkAjax is not defined’
- Uncaught TypeError: window.jsObj.closeWebView is not a function
- sencha touch中出现JS错误Uncaught TypeError: Object [object Object] has no method 'getForm'
- js调用java方法的遇到的坑Uncaught TypeError: Object [object Object] has no method