Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting alert when closing the browser

Status
Not open for further replies.

ravula2000

IS-IT--Management
Mar 8, 2002
205
US
Can I generate an alert box when I try to close the window that is started by browser, not by window.open() method?
Iam thinking to trap unload event of the window.. but dont know where to trap..
Can some body suggest me..?

Thanks
 
Hi,
But Iam not able to prevent window from closing..
After the window is closed Iam getting that message.
Can you please suggest some thing..


Thanks
 
use the
Code:
onbeforeunload
event instead of
Code:
onunload
, you'll be able to cancel closing by making your function return false or by typing
Code:
event.returnValue=false;
in your function.
Water is not bad as soon as it stays out human body ;-)
 
Thanks
Its working..

Thanks for every thing...
 
Hi,
I got one more problem. When I refresh the browser Iam getting this alert box generated from onbeforeunload event.
Can I prevent this? I want to get only this message when I try to close the window.

Thanks
 
The only thing you can do is detecting the reason for wich your page is being unloaded. If it's by F5 (that you can catch by "onkeyDown" event on the body), put the showMessage global boolean to false. Then in the onbeforeunload method, do not affect anything to the returnvalue if showmessage = false.
This way doesnt prevent your message from being shown if the user clicks the "refresh" button.

NOTE: the onbeforeunload event is also raised as soon as the user clicks on a link that changes current page. so you'll have to do the same thing for all your links. Water is not bad as soon as it stays out human body ;-)
 
Hi targol,
I will try and let you know..
Thanks
 
Hi Targol,
I tried but could not able to capture key down event.
Can you please help me.

Thanks
 
I managed to do it by this code :
Code:
<script language=&quot;Javascript&quot; type=&quot;text/javascript&quot;>

var showMessage=true;
	
function testerTouche() {
	var valTouche = window.event.keyCode;
	alert (&quot;valtouche =&quot; + valTouche );
	if (valTouche == 116) {
		showMessage=false;
	}
	return false;

</SCRIPT>

</head>

<body onkeydown='testerTouche();'>
...
</body>
Water is not bad as soon as it stays out human body ;-)
 
But only the thing is in this key down event if keycode=116 then I want to prevent the unload of the document.. not showing a message.
Is it possible?

Thanks
 
You can't stop page refresh, function key presses can't be canceled but once you've set your showMessage boolean to false, let the user refresh the page without message. Water is not bad as soon as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top