Changing the Android Webview hash without reloading page
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 开发者_如何学Goof these?
Thanks in advance.
M
Or if you just want to change the hash, you can use:
view.loadUrl("javascript:location.hash=\"#" +fragment+"\"");
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
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);
}
});
精彩评论