javascript window.close() 在 android webview 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23211722/
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
window.close() not working in android webview?
提问by Abhinav
Android 4.1 Eclipse Testing on device
设备上的 Android 4.1 Eclipse 测试
I am trying to open a html/javascript webpage, hosted on my localhost for now, in my android application through a webview. The page is suppose to just close after 2 sec through the below code
我正在尝试通过 webview 在我的 android 应用程序中打开一个 html/javascript 网页,该网页目前托管在我的本地主机上。该页面假设通过以下代码在 2 秒后关闭
<script type="text/javascript">
alert('hello');
setInterval(function(){window.close();},2000);
</script>
I am able to get the hello alert by using WebChromeClient. But the window.close is still not working. Any help would be appreciated.
我可以通过使用 WebChromeClient 获得 hello 警报。但是 window.close 仍然无法正常工作。任何帮助,将不胜感激。
PS: Here is how i am loading the URL:
PS:这是我加载 URL 的方式:
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl("http://192.168.1.137/abc.html");
webView.setWebChromeClient(new WebChromeClient());
采纳答案by Siddharth_Vyas
Try this :
试试这个 :
WebChromeClient webClient = new WebChromeClient(){
public void onCloseWindow(Window w){
super.onCloseWindow(w);
Log.d(TAG, "Window close");
}
};
Description :
描述 :
public void onCloseWindow (WebView window)
Notify the host application to close the given WebView and remove it from the view system if necessary. At this point, WebCore has stopped any loading in this window and has removed any cross-scripting ability in javascript.
通知宿主应用程序关闭给定的 WebView 并在必要时将其从视图系统中删除。此时,WebCore 已停止在此窗口中加载任何内容,并删除了 javascript 中的任何跨脚本功能。
回答by Egor Shloma
It's not possible on android version > KITKAT. But I have find solution. If android build version > KITKAT, I have detected message from js on server side in method onConsoleMessage of WebChromeClient class http://developer.android.com/reference/android/webkit/WebChromeClient.html. It works for me.
在 android 版本 > KITKAT 上是不可能的。但我已经找到了解决方案。如果android构建版本> KITKAT,我在WebChromeClient类http://developer.android.com/reference/android/webkit/WebChromeClient.html的onConsoleMessage方法中检测到来自服务器端js的消息。这个对我有用。