Javascript 安卓网络视图“;位置。替换“替换”;不';行不通

Javascript 安卓网络视图“;位置。替换“替换”;不';行不通,javascript,android,android-webview,html5-history,Javascript,Android,Android Webview,Html5 History,我有一个Android webview,其中一个页面使用location.replace(url)重定向到另一个页面 假设我有一个页面“A”,它重定向到页面“B”(使用location.replace)。当从“B”页按下“后退”按钮时,页面返回到“A”页,这将再次重定向到“B”页。 当我调试历史api(history.length)时,我可以清楚地看到,在页面“B”上,长度在“1”中增加(仅在Android 4.X webview上增加,在iOS/web浏览器/Android 2.X上保持不变)

我有一个Android webview,其中一个页面使用
location.replace(url)
重定向到另一个页面
假设我有一个页面“A”,它重定向到页面“B”(使用location.replace)。当从“B”页按下“后退”按钮时,页面返回到“A”页,这将再次重定向到“B”页。 当我调试历史api(history.length)时,我可以清楚地看到,在页面“B”上,长度在“1”中增加(仅在Android 4.X webview上增加,在iOS/web浏览器/Android 2.X上保持不变),这是一个bug!(location.replace不应更改历史记录。lengh!)

请尝试这种方法

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView mainWebView = (WebView) findViewById(R.id.webView1);

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        mainWebView.loadUrl("file:///android_asset/www/A.html");
    }

或者从中获得帮助并链接

我与Yaniv一起参与了这个项目,我们找到了问题的原因,当我们尝试添加
mailto:
链接时,我们引入了该问题,并根据进行了处理

答案建议使用以下WebViewClient扩展类:

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {     
        if(MailTo.isMailTo(url)){
            MailTo mt = MailTo.parse(url);
            // build intent and start new activity
            return true;
        }
        else {
            view.loadUrl(url);
            return true;
        }
    }
}
问题在于,显式地告诉
WebViewClient
加载URL并返回true(意思是“我们处理了这个”)会将页面添加到历史记录中。WebView能够自行处理常规URL,因此返回false并且不接触视图实例将允许WebView加载页面并按其应该的方式处理它

因此:

函数位置替换(url){
if(history.replaceState){
replaceState(null,document.title,url);
历史。go(0);
}否则{
位置.替换(url);
}
}
public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {     
        if(MailTo.isMailTo(url)){
            MailTo mt = MailTo.parse(url);
            // build intent and start new activity
            return true;
        }
        else {
            return false;
        }
    }
}