android4.4 WebView invoke JavaScript to appear uncaught Referenceerror:is not define or has no method

Source: Internet
Author: User
Tags object object

These days with the NEXUS5 4.4.4 system to do a simple mobile phone device number to get, and then invoke JavaScript display in the Web page function, has done more than n similar programs, the results of the program a run any problems are out, hehe

[Info:console (1)] "Uncaught Referenceerror:is not define

I/chromium (490): [Info:console (1)] "uncaught syntaxerror:unexpected token

[Android] Web console:uncaught Typeerror:object [Object Object] has no method ' xxx '


The conclusion is that the above problem, first look at the code


Demo.html

 



Take a look at 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, I think is not 4.4 of the system in writing with a low version of the same, or the use of 2 ways to invoke:

Mwebview.evaluatejavascript
Mwebview.loadurl ("Javascript:getdeviceid (' Maomao ')");

The final judgment is one possibility: when calling the Getdeviceid method, JS is not loaded.

Solution :

	Private class Webviewclientdemo extends Webviewclient {
		@Override public
		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);
		      Execute the JS function
		      if (!flag_get_deviceid) {
		    	  loadurl ()}
		  } that you want to call here
	

We are in the onpagefinished, all of the JS must be loaded, and then the implementation of Loadurl method, it can be.


Finally, incidentally, the benefits of Webview.evaluatejavascript, this method, you can get the JS function return results 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

Related Article

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.