是否可以使用 Javascript 来关闭 Android 浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7164242/
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
Is it possible to use Javascript to close Android Browser?
提问by sees
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.).
我想在网页中放置一个“关闭”按钮(我们的客户想要这样做)
,当我单击此按钮时,我想关闭浏览器(不是当前选项卡,而是 Android 浏览器、IE、Firefox 中的“浏览器”,铬等)。
I've searched around and found a method: window.close()
but seems to only work on IE.
My question is:
我四处搜索并找到了一种方法:window.close()
但似乎只适用于 IE。
我的问题是:
Is there any way to close Android Browser using Javascript?
有没有办法使用Javascript关闭Android浏览器?
回答by SLaks
This is not possible, and never will be.
这是不可能的,也永远不会。
回答by Piskvor left the building
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:
不 - 这是一件好事:该网页没有与浏览器本身混淆(“等等,我的窗口去哪里了?我有 30 个标签在那里 - 噗,消失了!”),更不用说一个明显的漏洞:
- insert an XSS into legitpage.example.com
- when it gets activated, open a pop-under window of evilpage.example.net, which looks just like legitpage.example.com
- close legitpage.example.com (if 2&3 are fast enough, the user may be fooled that xe's still on legitpage.example.com)
- ???
- Profit!
- 将 XSS 插入到 legitpage.example.com
- 当它被激活时,打开一个evilpage.example.net的弹出窗口,它看起来就像legitpage.example.com
- 关闭legitpage.example.com(如果2&3足够快,用户可能会被骗到xe仍在legitpage.example.com上)
- ???
- 利润!
回答by Oak Bytes
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.
Android 浏览器只允许 JavaScript 关闭弹出窗口。因此没有办法关闭窗口,除非它被创建为一个弹出窗口。
回答by gregko
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:
好吧,一个简单的解决方法是创建一个带有全屏 WebView 控件的活动,在那里显示您的 HTML 内容(本地或来自 Internet),并添加一个按钮来关闭此窗口,并回调到 Java 以关闭这个活动。这是您需要的所有代码:
browser.xml layout file:
browser.xml 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#404040"
android:id="@+id/mainLayout"
>
<WebView android:id="@+id/webkit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:scrollbars="vertical"
android:visibility="visible"
/>
</LinearLayout>
myBrowser.java activity (remember to declare it also in AndroidManifest.xml):
myBrowser.java 活动(记住也在 AndroidManifest.xml 中声明它):
public class myBrowser extends Activity {
protected WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// if you don't want app title bar...
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.browser);
webView = (WebView) findViewById(R.id.webkit);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaCallback(), "JCB");
// get the URL to navigate to, sent in Intent extra
Intent intent = getIntent();
if (intent != null) {
String sUrl = intent.getStringExtra("url");
if (sUrl != null)
webView.loadUrl(sUrl);
}
}
final public class MyJavaCallback {
// this annotation is required in Jelly Bean and later:
@JavascriptInterface
public void finishActivity() {
finish();
}
}
}
The code to start this activity elsewhere in your app would be:
在你的应用程序的其他地方启动这个活动的代码是:
Intent intent = new Intent(context, myBrowser.class);
intent.putExtra("url", "http://www.someaddress.com/somepage.html");
startActivity(intent);
and your somepage.html web page could have a "Close" button with the following code:
并且您的 somepage.html 网页可能有一个带有以下代码的“关闭”按钮:
<button onclick="JCB.finishActivity()">Close</button>
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.:
您还可以为它们添加其他按钮和回调,以执行您的 Android Java 代码所需的操作。以另一种方式调用 - 从 Java 到页面上的 JavaScript,也是可能的,例如:
webView.loadUrl("javascript:functionName(params)");
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:
此外,如果您想显示来自 Internet 的内容,而不是 WebView 控件中的本地文件或字符串,请记住向 AndroidManifest.xml 添加必要的权限:
<uses-permission android:name="android.permission.INTERNET" />
Greg
格雷格
回答by idmadj
window.close()
actually works for Web Apps that were added to the home screen (with Chrome Beta).
window.close()
实际上适用于添加到主屏幕的 Web 应用程序(使用 Chrome 测试版)。
It cleanly closes the app and gets back to the home screen.
它干净地关闭应用程序并返回主屏幕。