Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
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 16-Oct-12 21:49pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 335
1 Nirav Prabtani 252
2 Richard Deeming 215
3 CHill60 170
4 _Amy 145
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,045
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 7 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100