Reputation: 305
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?
Upvotes: 3
Views: 13883
Reputation: 5822
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
Upvotes: 1
Reputation: 2645
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.
Upvotes: 3
Reputation: 4795
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.
Upvotes: 1
Reputation: 92792
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:
Upvotes: 4