gpt4 book ai didi

javascript - 在 Android 浏览器中捕获浏览器关闭事件

转载 作者:行者123 更新时间:2023-11-29 22:17:55 26 4
gpt4 key购买 nike

如何在 Android 浏览器中捕获浏览器关闭或更具体的页面卸载事件?我无法通过 unloadbeforeunload 等事件使其在 Android 浏览器中运行。

最佳答案

    /* Tell the client to display a dialog to confirm navigation away from
the current page. This is the result of the onbeforeunload javascript event.
If the client returns true, WebView will assume that the client will handle
the confirm dialog and call the appropriate JsResult method. If the client
returns false, a default value of true will be returned to javascript to accept
navigation away from the current page. The default behavior is to return false.
Setting the JsResult to true will navigate away from the current page, false will
cancel the navigation.
*/
public boolean onJsBeforeUnload (WebView view, String url, String message, JsResult result){

}

来自 http://developer.android.com/reference/android/webkit/WebChromeClient.html

我认为您还可以将代码注入(inject)到 WebView 显示的网页中:

<script type="text/javascript">
function OnBeforeUnLoad () {
return "";
}
</script>

使用

@Override  
public void onPageFinished(WebView view, String url)
{
webview.loadUrl("string with your script");
}

关于javascript - 在 Android 浏览器中捕获浏览器关闭事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8064929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com