Is it possible to use Javascript to close Android Browser?

前端 未结 5 756
有刺的猬
有刺的猬 2021-01-21 05:03

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 An

相关标签:
5条回答
  • 2021-01-21 05:26

    This is not possible, and never will be.

    0 讨论(0)
  • 2021-01-21 05:29

    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!
    0 讨论(0)
  • 2021-01-21 05:30

    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.

    0 讨论(0)
  • 2021-01-21 05:35

    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.

    0 讨论(0)
  • 2021-01-21 05:39

    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

    0 讨论(0)
提交回复
热议问题