是否可以使用Javascript关闭Android浏览器?
我想在网页中放置一个“关闭”按钮(我们的客户想要这样做)
当我单击此按钮时,我想关闭浏览器(不是当前选项卡,而是 Android 浏览器、IE、Firefox、Chrome 等中的“浏览器”)。
我四处搜索并找到了一个方法:window.close()
,但似乎只适用于 IE。
我的问题是:
有什么方法可以使用 Javascript 关闭 Android 浏览器吗?
I want to put a "close" button in a web page (our client wants to do that)
and when I click this button, I want to close Browser (not the current tab but "browser" in Android Browser, IE, Firefox, Chrome etc.).
I've searched around and found a method: window.close()
but seems to only work on IE.
My question is:
Is there any way to close Android Browser using Javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不 - 这是一件好事:网页与浏览器本身没有任何关系(“等等,我的窗口去哪里了?我那里有大约 30 个选项卡 - 噗,不见了!”),更不用说一个明显的漏洞了:
Nope - and that's a Good Thing: the webpage has no business messing with the browser itself ("wait, where did my window go? I had like 30 tabs in there - poof, gone!"), not to mention a glaring vulnerability:
这是不可能的,也永远不会。
This is not possible, and never will be.
window.close()
实际上适用于添加到主屏幕的 Web 应用(使用 Chrome Beta)。它干净地关闭应用程序并返回主屏幕。
window.close()
actually works for Web Apps that were added to the home screen (with Chrome Beta).It cleanly closes the app and gets back to the home screen.
Android 浏览器允许 JavaScript 仅关闭弹出窗口。因此,除非将窗口创建为弹出窗口,否则无法关闭窗口。
Android browser allows JavaScript to close only popup windows. Hence there is no way to close window, unless it has been created as a popup window.
那么,一个简单的解决方法是创建一个具有全屏 WebView 控件的活动,在那里显示您的 HTML 内容(本地或来自 Internet),并添加一个按钮来关闭此窗口,并回调 Java 来关闭这项活动。以下是您需要的所有代码:
browser.xml 布局文件:
myBrowser.java 活动(请记住也在 AndroidManifest.xml 中声明它):
在应用程序中其他位置启动此活动的代码将是:
和您的 somepage.html 网页页面可以有一个带有以下代码的“关闭”按钮:
您还可以添加其他按钮和回调,以执行 Android Java 代码中所需的操作。也可以以其他方式调用 - 从 Java 到页面上的 JavaScript,例如:
另外,如果您想显示来自 Internet 的内容,而不是 WebView 控件中的本地文件或字符串,请记住向 AndroidManifest.xml 添加必要的内容许可:
格雷格
Well, a simple work-around for this would be to create an activity with full screen WebView control, display your HTML contents (local or from the Internet) there, and add a button to close this window, with a callback to Java to close this activity. Here is all the code you would need:
browser.xml layout file:
myBrowser.java activity (remember to declare it also in AndroidManifest.xml):
The code to start this activity elsewhere in your app would be:
and your somepage.html web page could have a "Close" button with the following code:
You may add as well other buttons and callbacks for them to do what's needed in your Android Java code. Calls the other way - from Java to JavaScript on the page, are also possible, e.g.:
Also, if you want to display content from Internet, not a local file or string in your WebView control, remember to add to AndroidManifest.xml the necessary permission:
Greg