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