Click here to Skip to main content
15,065,044 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I have to check some condition on browser close event means When i close the browser ( when i click on [x] ) that event i want using javascript.
i am able to do this in IE but in firfox itis not working properly
i tried

var altKey  = false;
var keyCode = 0;

function closeSession(evt){

    evt = (evt) ? evt : event;

    clickY  = evt.clientY;
    altKey  = evt.altKey;
    keyCode = evt.keyCode;

        // Window Closing in FireFox
        // capturing ALT + F4
        keyVals = document.getElementById('ffKeyTrap');
        if(keyVals.value == 'true115'){
            return 'close 1';

        if(keyVals.value == ''){
            // capturing a window close by "X" ?
            // we have no keycodes
            return 'close 2';

    } else {
        // Window Closing in IE
        // capturing ALT + F4
        if (altKey == true && keyCode == 115){
            alert('close 1');
        // capturing a window close by "X"
        } else if(clickY < 0){
            alert('close 2');
        // simply leaving the page via a link
        } else {
            //alert('close 3');
            return void(0);

function whatKey(evt){
    evt = (evt) ? evt : event;
    keyVals = document.getElementById('ffKeyTrap');
    altKey  = evt.altKey;
    keyCode = evt.keyCode;
    if(altKey && keyCode == 115){
        keyVals.value = String(altKey) + String(keyCode);

window.onkeydown      = whatKey;
window.onbeforeunload = closeSession;
Updated 28-May-14 22:36pm
Alok.singh1166 29-May-14 4:20am
some body please help me

1 solution

Please try as below.

<script type="text/javascript">
window.onbeforeunload = check;
function check()
return "Are you sure you want to exit this page?";
//or put whatever function you need to call when a user closes the web //browser.

Read for more : how to detect mozilla firefox window close using javascript

Close Event In Javascript For Firefox, IE and Chrome
Alok.singh1166 29-May-14 5:13am
i already tried this .But it is not working with firfox

Please Help
Sampath Lokuge 29-May-14 9:54am
Check whether is there any js errors on your code by using firebug ?
Alok.singh1166 30-May-14 1:02am
Sampath there is no error what ever code you have provided and i have tried is working fine in IE or Chrome but not working in FireFox. after FF 4.0 no js file is supported to mozialfirefox.

Please check.

Alok Singh

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900