These days with NEXUS5 4.4.4 system to do a simple mobile phone device number to get, and then call JavaScript display in the Web page function, previously done n many similar programs, the results of a program run what problems are out, hehe
[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 '
In summary, this is the problem, look at the code first
Demo.html
Check out my Android code below:
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 ')");}}
When the Loadurl method is in the OnCreate method of activity, Mwebview.loadurl ("file:///android_asset/demo.html"); Call Loadurl () after execution;Basically the above error, I think is not 4.4 of the system in writing with the lower version of the same, or the use of 2 ways to call:
Mwebview.evaluatejavascript
Mwebview.loadurl ("Javascript:getdeviceid (' Maomao ')");
The final judgment is a possibility: when invoking the Getdeviceid method, JS is not loaded.
Workaround :
Private class Webviewclientdemo extends Webviewclient {@Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {log.d (TAG, "URL:" +url); View.loadurl (URL);////When a new link is opened, the current WebView is used and no other browser of the system is used to return true;} @Override public void onpagefinished (WebView view, String URL) { super.onpagefinished (view, URL); Here execute the JS function you want to invoke if (!flag_get_deviceid) { loadurl ();}} }
When we onpagefinished, all of the JS must be loaded, at this time to execute Loadurl method, you can.
Finally, incidentally, the advantage of Webview.evaluatejavascript, this method, you can get the return result of the JS function to:
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;}}} );
- Join CSDN Technology Group: 221057495 Communicate with me
android4.4 webview call JavaScript appears uncaught Referenceerror:is not define or has no method