Android WebView Custom Error page notes

Source: Internet
Author: User

Demand:

WebView Load H5 page encountered an error, you need to modify the system default page, the use of their own customized pages.

Solutions and Procedures:

1. The first use of Webview.loaddatawithbaseurl is to load a string:

String data= "<a onclick=\" window.history.back () \ "> Page error, click Refresh </a>";

Mwebview.loaddatawithbaseurl ("", Data, "text/html", "UTF-8", "" ");

There is a problem: because the Android4.4 system has replaced the browser kernel (from WebKit to Chrome), Window.history.back () is experiencing a problem (WebKit is inconsistent with Chrome's back). Therefore, this method needs to be modified.

2. Then try the following code:

String data= "<a onclick=\" window.location.replace (\ ' "+failingurl+" \ ') \ "> Page error, click Refresh </a>";

Mwebview.loaddatawithbaseurl ("", Data, "text/html", "UTF-8", "" ");

There is a problem: This method avoids the Window.history.back method, but when the user clicks "Refresh" and then backs back the page, it returns to the error page.

3. The final choice:

String data= "<a href=\" javascript:location.reload () \ "> Page error, click Refresh </a>";

Webview.loadurl ("javascript:document.body.innerhtml=\" "+ sb.tostring () +" \ "");

Use innerHTML page does not occur jump, so directly call JS Reload method can, both evade the History.back method, the user will not return to the error page again

Android WebView Custom Error page notes

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.