Changing the Android Webview hash without reloading page

谁都会走 提交于 2020-01-03 22:54:09

问题


I'm got a custom webview setup which is working pretty well, but I'd like to be able to either:

1, change the url hash without the webview reloading the page (it would lose the state of my js app)

2, call some js that sits within my web page from within android. I can't change any JS within the site, unfortunately, so can't custom write any js to put on the site especially for the job, the only stuff I have control over is the Android app.

Can anyone think of a way of doing either of these?

Thanks in advance.

M


回答1:


For #2, your Activity can invoke JavaScript methods. All you have to do is call the loadUrl method with the appropriate JavaScript call:

mWebView.loadUrl("javascript:wave()");

From: http://developer.android.com/resources/articles/using-webviews.html




回答2:


Or if you just want to change the hash, you can use:

view.loadUrl("javascript:location.hash=\"#" +fragment+"\"");



回答3:


I know this question is old but this is what works for me in Android 4.4:

String fragment = "#test"
mWebView.evaluateJavascript("location.hash=\"" + fragment + "\";", new ValueCallback<String>() {
     @Override
     public void onReceiveValue(String value) {
         // Yes, I like to log data :-)
         Log.d("MyApp", "onReceiveValue(value): " + value);
     }
});


来源:https://stackoverflow.com/questions/2774923/changing-the-android-webview-hash-without-reloading-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!