Android4.4 when webview calls javascript, the following error occurs: Uncaught ReferenceError: is not define or has no method, webviewuncaught.

Source: Internet
Author: User

Android4.4 when webview calls javascript, the following error occurs: Uncaught ReferenceError: is not define or has no method, webviewuncaught.

In the past few days, we have used the Nexus5 4.4.4 system to obtain a simple mobile phone device number, and then called the javascript display function on the webpage. We have done n similar programs before, as a result, all the problems with the program were solved.

[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'


To sum up, let's look at the code first.


Demo.html

<Html xmlns = "http://www.w3.org/1999/xhtml"> 



Check out my android Code as follows:

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 the activity, mWebview. loadUrl ("file: // android_asset/demo.html"); Call loadUrl () after execution ();

Basically, the above error occurs. I wonder if the 4.4 system is written differently from the lower version, or two call methods are used:

mWebview.evaluateJavascript
mWebview.loadUrl("javascript:getDeviceID('maomao')");

The final judgment is a possibility: js is not loaded when the getDeviceID method is called.

Solution:

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. other browsers of the system do not return true;} @ Override public void onPageFinished (WebView view, String url) {super. onPageFinished (view, url); // execute the js function if (! Flag_get_deviceid) {loadUrl ();}}}

When we are in onPageFinished, all js loads must have been completed. Then we can execute the loadUrl method.


Finally, let's take a look at the benefits of webview. evaluateJavascript. This method can get the return result of the js function:

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 contact me


In android code, how does webview call JavaScript?

MWebView. setWebChromeClient (new MyWebChromeClient (); mWebView. addJavascriptInterface (new Object () {/*** This is not called on the UI thread. post a runnable to invoke * loadUrl on the UI thread. */public void clickOnAndroid () {mHandler. post (new Runnable () {public void run () {mWebView. loadUrl ("javascript: wave ()") ;}}}, "demo"); similarly, there must be an object named demo in html that calls clickOnAndroid () method. /*** Provides a hook for calling "alert" from javascript. useful for * debugging your javascript. */final class MyWebChromeClient extends WebChromeClient {@ Override public boolean onJsAlert (WebView view, String url, String message, JsResult result) {result. confirm (); return super. onJsAlert (view, url, message, result );}}
 
In android development, WebView loads html webpages with flash. Why is it invalid when javascript is called by flash effectively on the computer?

Enablejavascript (true) must be set for webview to allow webview to execute javascript

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.