0

I need a way to close the browser (not a pop up) using Javascript. Is it possible?

2

5 Answers 5

7

If the window isn't opened by some JavaScript code from your own domain, then you can't close it in a crossbrowsercompatible way using JavaScript. Only in MSIE you can use window.close(), but this will generate a warning which is however workaroundable by calling window.open('', '_self', ''); beforehand. Still, this won't work in Firefox and other decent browsers. And with a good reason. You shouldn't forcibly close windows which the enduser opened itself. This is Bad User Experience.

Sign up to request clarification or add additional context in comments.

2 Comments

"won't work on other decent browser"... i like what this implies :D
This isn't necessarily a bad user experience: I am running a user study; user performs task; at end of task, I want the user to do something else. When the task is done, the user should press the 'done' button & have the window go away.
3

you can use this -

< a href="javascript:window.opener='x';window.close();">Close< /a>

usually when you use window.close() on main browser window, it shows a message saying "Page is trying to close window". To avoid this, set the window.opener to any value. Idea is window.opener should not be null or empty.

3 Comments

Might not always work. For ref, check this : developer.mozilla.org/en/window.close
@Rajat - Yes you are right. it still asks for a prompt to close the window
window.opener expects a specific type. Setting it to a string or char will cause a javascript error.
1

The whole browser? Generally, no. You can close the current tab with window.close(), but it will prompt the user before closing the browser (unless it was created with window.open()). This is a security feature; it would be unpleasant if an arbitrary webpage could close your browser unbidden.

Comments

1

In general, you can close a window that you yourself created using javascript.

You can't close any other window, not even the window in which your page is loaded. I remember that some browser actually ask "This web page is try to close your window. Allow?" or something of this sort. This is not something that you want your end user to see.

BTW, if you specify your particular use case, I could suggest alternate solutions.

Comments

1

try this,

    <a href="" OnClick="window.close()">[CLOSE]</a>

It'll close window for sure

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.