最近在做关于WebView与h5混合开发的项目,遇到各种问题,今天一个问题是在WebView中加载页面的时候,js获取不到document.referrer,从而无法点击web页面的返回键返回到上一个页面。最终发现一个解决办法,如果对URL没有特定的约束则可以,但是如果后面对load的URL有特殊处理的话,需要慎重考虑。
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// view.loadUrl(url);
// return true;
//因为JS返回控制得不到document.referrer,所以解决方案为返回false,目前对其他没有影响副作用待查
return false;
}
该方法重写是为了在loadurl的时候不跳转到浏览器,查看源码发现其解释为下,大体意思是当放回true时说明是该应用操作管理load的URL,返回FALSE的时候是webview操纵管理该URL。
Give the host application a chance to take over the control when a new * url is about to be loaded in the current WebView. If WebViewClient is not