开发者

Is it possible to close a browser window using javascript? [duplicate]

This question already has answers here: How can I close a browser window without receiving the "Do you want to close this window" prompt? (18 answers) Clo开发者_开发知识库sed 6 years ago.

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?


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.


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.


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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜