Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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?

like image 717
ashishjmeshram Avatar asked Nov 30 '25 01:11

ashishjmeshram


2 Answers

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.

like image 150
ashishjmeshram Avatar answered Dec 02 '25 15:12

ashishjmeshram


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.

like image 22
Carl R Avatar answered Dec 02 '25 15:12

Carl R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!