admin管理员组文章数量:1290979
A button in my WebView
is used to go back using the history.back()
JavaScript call. I do not understand much of JavaScript, but after a bit of searching I found that we can use the addJavascriptInterface()
method of a WebView
.
Now, I intend to finish my Activity when the button is clicked. I landed up having something of this sort:
public class MyActivity extends Activity {
private static final String sTag = "MyActivity";
private WebView mWebContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_page);
mWebContent = (WebView) findViewById(R.id.webContent);
mWebContent.getSettings().setJavaScriptEnabled(true);
mWebContent.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebContent.getSettings().setBuiltInZoomControls(true);
mWebContent.addJavascriptInterface(new JavaScriptInterface(), "history"); //history.back();
Bundle extras = getIntent().getExtras();
if (extras != null) {
mWebContent.loadUrl(extras.getString("URL"));
}
}
public class JavaScriptInterface {
JavaScriptInterface() {
}
public void back() {
Log.v(sTag, "back pressed");
MyActivity.this.finish();
}
}
}
But unfortunately when the button is pressed the Activity doesn't finish, neither do I get anything in the Log.
Here is the html for the button :
<a href="javascript:void(0);">
<img src="images/btn-go-back.png" onClick="history.back();" border="0" />
</a>
What am I doing wrong?
Thanks in advance.
Edit : Just wanted to clear the fact that changing the html is not an option for me. I have no power over the the html code :). I have to handle it in the application.
Edit: Tried changing history.back()
to window.history.back()
(locally), still no change.
Edit : I was experimenting by locally loading html conent and found out that if I change history.back()
to say something like android_app.back()
and register my JavaScriptInterface
by the nameandroid_app
it works fine. Why is that? Does that mean that we can't use history
to register an interface? Does the developer docs mention this?
A button in my WebView
is used to go back using the history.back()
JavaScript call. I do not understand much of JavaScript, but after a bit of searching I found that we can use the addJavascriptInterface()
method of a WebView
.
Now, I intend to finish my Activity when the button is clicked. I landed up having something of this sort:
public class MyActivity extends Activity {
private static final String sTag = "MyActivity";
private WebView mWebContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_page);
mWebContent = (WebView) findViewById(R.id.webContent);
mWebContent.getSettings().setJavaScriptEnabled(true);
mWebContent.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebContent.getSettings().setBuiltInZoomControls(true);
mWebContent.addJavascriptInterface(new JavaScriptInterface(), "history"); //history.back();
Bundle extras = getIntent().getExtras();
if (extras != null) {
mWebContent.loadUrl(extras.getString("URL"));
}
}
public class JavaScriptInterface {
JavaScriptInterface() {
}
public void back() {
Log.v(sTag, "back pressed");
MyActivity.this.finish();
}
}
}
But unfortunately when the button is pressed the Activity doesn't finish, neither do I get anything in the Log.
Here is the html for the button :
<a href="javascript:void(0);">
<img src="images/btn-go-back.png" onClick="history.back();" border="0" />
</a>
What am I doing wrong?
Thanks in advance.
Edit : Just wanted to clear the fact that changing the html is not an option for me. I have no power over the the html code :). I have to handle it in the application.
Edit: Tried changing history.back()
to window.history.back()
(locally), still no change.
Edit : I was experimenting by locally loading html conent and found out that if I change history.back()
to say something like android_app.back()
and register my JavaScriptInterface
by the nameandroid_app
it works fine. Why is that? Does that mean that we can't use history
to register an interface? Does the developer docs mention this?
3 Answers
Reset to default 3Read the Navigating web page history section of this article. It tells pletely about the android webviews.It tells about how to make webviews handle history.
http://developer.android./guide/webapps/webview.html
Hope this helps.
Try
<a href="#" onclick="window.history.back()"><img src="bla" /></a>
edit: just read you can't change the HTML code .. Don't see a solution then ..
After a lot of searching and asking I have finally arrived at the conclusion that you cannot add a java script interface, using addJavascriptInterface (Object obj, String interfaceName)
, with the interface name as history
(or perhaps any javascript keyword).
Maybe this is written somewhere in the developer docs or manual, but I couldn't find it though.
本文标签: javascriptAndroidDetect historyback() in WebViewStack Overflow
版权声明:本文标题:javascript - Android : Detect history.back() in WebView - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741498905a2381964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论