Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

My Boss have asked me to kill the ALT+F4 process OR to give user the prompt of asking it to close or not through JavaScript if some one tries to close the Web Application while the App is logged in.

I mean if a user has already logged in into the Web Application and if he tries to close it by ALT+F4 then it doesn't get closed (prompt either).

I have captured the ALT+F4 event through Javascript and tried to use the confirm box but the event (ALT+F4) then also runs and closes the application.

I am adding the code i have used here.
<html>
<head>
<title>Detecting browser close in IE</title>


<script type="text/javascript">

var myclose = false;
document.onkeydown = keydown; 

function keydown(evt)
{ 
 if (!evt) 
  evt = event;
  if (evt.altKey && evt.keyCode==115)
  { 
  // ALT+F4 
  //alert("ALT+F4");
    var r=confirm("Do you really want to close the application");
    if(r==true){
      alert("Closing");
    }
    else{
      alert("not closing");
    }
    evt.returnValue = false;
    return false;
  } 
} 

// function keydown(evt)

function ConfirmClose()
{
  if (event.clientY < 0)
  {
    event.returnValue = 'Any message you want';
    setTimeout('myclose=false',100);
    myclose=true;
  }
}

function HandleOnClose()
{
  if (myclose==true) alert("Window is closed");
}

</script>
</head>

<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">

<h4>Close browser!</h4>

</body>
</html>


Any help would be appreciable.

Thanks

Varun Sareen
Posted

<html>
<head>
<title>Detecting browser close in IE</title>


<script type="text/javascript">

var myclose = false;
document.onkeydown = keydown; 

function keydown(evt)
{ 
 if (!evt) 
  evt = event;
  if (evt.altKey && evt.keyCode==115)
  { 
    windows.onbeforeunload = Function(){
          return false;
    }
  } 
} 

// function keydown(evt)

function ConfirmClose()
{
  if (event.clientY < 0)
  {
    event.returnValue = 'Any message you want';
  }
}

function HandleOnClose()
{
  if (myclose==true) alert("Window is closed");
}

</script>
</head>

<body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()">

<h4>Close browser!</h4>

</body>
</html>


This is the solution to my above problem that i found myself but now i want to handle the control menu option event in case of browser closure.

Any help would be appreciable.

Thanks

Varun Sareen
 
Share this answer
 
Thanks, but re Solution 1, please test yr code (or say if you haven't).

It's window.onbeforeunload not windows.onbeforeunload -- no "s".

Also
if (evt.altKey && evt.keyCode==115)
is too late.. window has already shut.
 
Share this answer
 
v2

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