Prevent users from accidentally closing window?

Prevent users from accidentally closing window?

  
Hi everyone!

I would just like to know if there is a way I could prevent users from closing the browser window?

I'm looking at javascripts that would be able to disable alt+ f4 and also window unload to show alert message that users are about to close the window but I can't make it to work.

I know disabling alt + f4 and close button of browser is an issue for cases that the browser freezes and there is no way to close the page but i really need to be able to prevent users from being able to close the window because data might be lost or they may trick the system im doing, resulting to resetting the timer of my certain page.

Do you have any Ideas on how I could handle this or a way to disable the buttons? Any suggestions are big help! Thanks in advance.
Hi,

This is just an opinion and feel free to disagree, but i really don't understand the need to do all that.
In web i think that the system we make should prevent all kind of tricks, but not disabling browser buttons or shortcut keys, there are other ways like validating sessions, encrypting parameters, check for duplicate records, and responding to your "because data might be lost" maybe you could create a table where you make autosave on change, so that you can recover previous state.

To make my point a little bit clear, imagine if you do that kind of disabling in that annoying windows that appear all time, or how can you prevent the user to shutdown the computer or even the lights go out?

Sorry for not responding to your question/doubt, but my opinion is that you have to think different, disabling everything seems to me that was the "easy" approach.
Hi Nuno!,

i agree to your opinion but I can't explain it fully, but the system im working on requires user not being able to close the window in a certain page because the system should record the 'Key performance Indicator' of users. and closing the window just breaks the KPI.

The system im working on is a big system, It handles BL(Bill of Ladings) processing and time of users processing a certain BL is recorded. The page already have pause and pend button for processing the BL but users might choose to close the window to cheat the system. Its just a precaution to disable alt + f4.

Users of the system have already been using a similar system before but it is windows based system and Users are used to using keyboard shortcuts and that includes 'Alt + F4'. If they accidentally pressed alt + f4 while doing a certain process, this would slow them down and lose $$$ along the way.

So I know, what i ask is a kind of absurd, clients asked for this and I cant do anything but to follow. So if any suggestions i can do for this particular problem would be greatly appreciated.
So after searching a LOT around the net. what i can do now is to just alert user everytime pressing 'alt'

what i used is this.

document.onkeypress = function (event) {
        event = (event || window.event);
        var key = event.keyCode;
        if (key == 18) {

            alert("this key is disabled");
            return false;
        }
        else {
          
            event.returnValue = true;
        }
    }
   
 
 
    document.onkeyup = function (event) {
        event = (event || window.event);
        var key = event.keyCode;
        if (key == 18) {

            alert("this key is disabled");
          return false;
        }
        else {
         
            event.returnValue = true;
        }
    }

    document.onkeydown = function (event) {
        event = (event || window.event);
        var key = event.keyCode;
        if (key == 18) {

            alert("this key is disabled");
          return false;
        }
        else {
          
            event.returnValue = true;
        }
    }

It seems to work. but maybe you have a better idea? thank you so much!