是否可以使用Javascript关闭Android浏览器?

发布于 2024-12-02 14:56:20 字数 245 浏览 0 评论 0原文

我想在网页中放置一个“关闭”按钮(我们的客户想要这样做)
当我单击此按钮时,我想关闭浏览器(不是当前选项卡,而是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

许一世地老天荒 2024-12-09 14:56:20

不 - 这是一件好事:网页与浏览器本身没有任何关系(“等等,我的窗口去哪里了?我那里有大约 30 个选项卡 - 噗,不见了!”),更不用说一个明显的漏洞了:

  1. 当legitpage.example.com被激活时,将XSS插入到legitpage.example.com中
  2. ,打开一个evilpage.example.net的弹出窗口,它看起来就像legitpage.example.com
  3. 关闭legitpage.example.com(如果2 和 3 足够快,用户可能会被愚弄,认为 xe 仍在 legalitpage.example.com 上)
  4. ???
  5. 利润!

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:

  1. insert an XSS into legitpage.example.com
  2. when it gets activated, open a pop-under window of evilpage.example.net, which looks just like legitpage.example.com
  3. close legitpage.example.com (if 2&3 are fast enough, the user may be fooled that xe's still on legitpage.example.com)
  4. ???
  5. Profit!
霞映澄塘 2024-12-09 14:56:20

这是不可能的,也永远不会。

This is not possible, and never will be.

小糖芽 2024-12-09 14:56:20

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.

黒涩兲箜 2024-12-09 14:56:20

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.

苏佲洛 2024-12-09 14:56:20

那么,一个简单的解决方法是创建一个具有全屏 WebView 控件的活动,在那里显示您的 HTML 内容(本地或来自 Internet),并添加一个按钮来关闭此窗口,并回调 Java 来关闭这项活动。以下是您需要的所有代码:

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 活动(请记住也在 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();
        }
    }
}

在应用程序中其他位置启动此活动的代码将是:

    Intent intent = new Intent(context, myBrowser.class);
    intent.putExtra("url", "http://www.someaddress.com/somepage.html");
    startActivity(intent);

和您的 somepage.html 网页页面可以有一个带有以下代码的“关闭”按钮:

<button onclick="JCB.finishActivity()">Close</button>

您还可以添加其他按钮和回调,以执行 Android Java 代码中所需的操作。也可以以其他方式调用 - 从 Java 到页面上的 JavaScript,例如:

webView.loadUrl("javascript:functionName(params)");

另外,如果您想显示来自 Internet 的内容,而不是 WebView 控件中的本地文件或字符串,请记住向 AndroidManifest.xml 添加必要的内容许可:

<uses-permission android:name="android.permission.INTERNET" />

格雷格

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:

<?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):

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:

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

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:

<uses-permission android:name="android.permission.INTERNET" />

Greg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文