android4.4 webview call JavaScript appears uncaught Referenceerror:is not define or has no method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.