2

I need to close the browser window using the java script.

I have tried using window.close but it is not working.

I have also googled and checked stack overflow for any similar posts. I have also gone through following link but in vain.

Is it possible to close browser window from JavaScript?

What I am trying to do is load a page and on load of the page call a java script method which does window.close

Below is the code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
function closeBrowser(){
    window.close();
}
</script>
</head>
 <body onload="closeBrowser();">
 </body>
</html>

It is properly working in IE8 though. Its not working in the fire fox.

And even in IE it gives the alert prompt before actually closing the browser. Can we avoid the alert in IE?

Why it does not work in fire fox?

Community
  • 1
  • 1
ajm
  • 12,863
  • 58
  • 163
  • 234

5 Answers5

7

The reason it was not working in fire fox was due to some settings in the fire fox.

Please set your firefox browser:

1.input "about:config " to your firefox address bar and enter;

2.make sure your "dom.allow_scripts_to_close_windows" is true

window.close is now working in fire fox too.

ajm
  • 12,863
  • 58
  • 163
  • 234
  • Why would you as a user do that to yourself? – Raynos Mar 28 '11 at 07:31
  • 1
    Yeah, that is not on by default, but of course if you have control over the installation you can allow a lot of things, normal users would not allow. – mplungjan Mar 28 '11 at 11:50
1

You have the first script tag twice, which might cause problems. You should also add the attribute type="text/javascript" so that you can be sure that the script is picked up by all browsers.

Carl R
  • 8,104
  • 5
  • 48
  • 80
  • Actually current best practice is under discussion to leave it off. It is JavaScript in all browsers by default – mplungjan Mar 28 '11 at 06:43
  • I know, but my experience has been that some browsers actually doesn't work well without it. I think it was FF in my case, but it was half a year ago so I don't remember the details. – Carl R Mar 28 '11 at 07:10
1

What you want to do is undesirable for the user.

If a user loads your page and you as a webmaster decides that the main browser needs to close, then they will likely get seriously annoyed if you succeeded. Window history and such, gone.

Please explain EXACTLY why you want this. For example if you need to log someone out use location.replace('logout.php)` and have that page log them out

There are hacks that I personally wish would be fixed - in this exact duplicate here: How can I close a browser window without receiving the "Do you want to close this window" prompt?

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Basically I need to close the browser window when the user logout of the application. – ajm Mar 28 '11 at 06:45
  • @Ashish: then either open your app using window.open or if you can do IE only: kiosk mode or HTA – mplungjan Mar 28 '11 at 06:47
  • Down voter needs to comment as to why. – mplungjan May 15 '15 at 08:20
  • 1
    @mplungjan Isn't it obvious? You mix in you personal views on morality of closing browser window(sic!), and this is purely technical site. Also, your attitude. – stroncium Apr 26 '16 at 02:28
  • So personal opinion and attitude is not possible on a Q&A site? I try to convey the attitude of users to a site that thinks denial of service is great UX. Something OP might not have considered. I also supply links to possible solutions. My loathing of having a widow closed on me has only diminished n the five years since this was written due to not having encountered it. – mplungjan Apr 26 '16 at 04:16
  • @stroncium ps: there are hundreds of X/Y problems at this site so asking exactly what op wants is not attitude but anticipation of a "I don't know how to stop the user from resubmitting my form" or similar - and I was correct: the actual issue is to log out – mplungjan Apr 26 '16 at 04:18
  • @mplungjan Well it is still much cleaner without asking the reasons. Closing browser tab on logout can be completely legit action which will not only not hurt user but help him. I for example came here for this very thing and trust me, it is justified. People use browsers not only for the sites you do a casual stroll around but as internal(and not so internal) tools, and it is actually quite sad we need to use things like node-webkit to provide such functionality. – stroncium Apr 26 '16 at 05:21
  • Closing a tab is perfectly fine! Trying to close a window you did not open yourself is not, hence the alerts OP wanted to suppress. The solution to logging out is not to close people's main browser but to navigate away - possibly using location.replace and have the new page close the session if necessary. The apps at my office do that all the time without any inconvenience to the user. – mplungjan Apr 26 '16 at 05:30
0

When using the close method you need to keep in mind an important thing: The close method closes only windows opened by javascript using the open method. Since you are trying to close the current window, you should use

 self.close();

Also, you should remember that, users will get a confirmation, asking the user to choose whether the window is to be closed or not

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
0

There a thousand things which work in IE but not in other browsers. I think window.close(); should server your purpose. It can be called on any event on your page.