javascript Android:在 WebView 中检测 history.back()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7190692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:15:04  来源:igfitidea点击:

Android : Detect history.back() in WebView

javascriptandroidwebkitwebview

提问by Arnab Chakraborty

A button in my WebViewis 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.

my 中的一个按钮WebView用于使用history.back()JavaScript 调用返回。我不明白很多的JavaScript,但有点搜索后,我发现,我们可以使用addJavascriptInterface()的方法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 :

这是按钮的html:

<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.

编辑:只是想澄清一个事实,即更改 html 不是我的选择。我对 html 代码没有任何权力:)。我必须在应用程序中处理它。



Edit:Tried changing history.back()to window.history.back()(locally), still no change.

编辑:尝试更改history.back()window.history.back()(本地),仍然没有更改。



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 JavaScriptInterfaceby the nameandroid_appit works fine. Why is that? Does that mean that we can't use historyto register an interface? Does the developer docs mention this?

编辑:我正在通过本地加载 html 内容进行试验,发现如果我更改history.back()为说类似的内容android_app.back()并按JavaScriptInterface名称注册我的内容,则android_app它可以正常工作。这是为什么?这是否意味着我们不能使用history注册接口?开发人员文档是否提到了这一点?

采纳答案by Arnab Chakraborty

After a lot of searching and asking I have finally arrived at the conclusion that you cannotadd a java script interface, using addJavascriptInterface (Object obj, String interfaceName), with the interface name as history(or perhaps any javascript keyword).

经过大量的搜索和询问,我终于得出结论,您不能添加 java 脚本接口, using addJavascriptInterface (Object obj, String interfaceName),接口名称为history(或任何 javascript 关键字)。

Maybe this is written somewhere in the developer docs or manual, but I couldn't find it though.

也许这是在开发人员文档或手册中的某处写的,但我找不到它。

回答by Sreeram

Read the Navigating web page historysection of this article. It tells completely about the android webviews.It tells about how to make webviews handle history.

阅读本文的导航网页历史记录部分。它完全讲述了android webviews。它讲述了如何使webviews 处理历史记录。

http://developer.android.com/guide/webapps/webview.html

http://developer.android.com/guide/webapps/webview.html

Hope this helps.

希望这可以帮助。

回答by Bart Blommaerts

Try <a href="#" onclick="window.history.back()"><img src="bla" /></a>

尝试 <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 ..

编辑:刚刚阅读您无法更改 HTML 代码.. 没有看到解决方案然后..

回答by Mahesh

Check this:

检查这个:

@Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }