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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alert before closing a window.

Status
Not open for further replies.

NathanA

ISP
Sep 23, 2009
20
US
I want to prevent a user from accidentally closing a browser window. I was curious if javascript could throw up a prompt window if someone clicks the X to close there browser.
 
Nope. Hitting the X will always close the window. cquick@callingpost.com
Geographic Information System (GIS), ASP, some Oracle
 
although the X will close the window, if you put in the body tag 'onunload=' or 'onbeforeunload=', your effect will be...

onunload= will call a function first, then close the window...

onbeforeunload= (only in ie, i believe) will call a function, let's say a confirm prompt, thereby alerting the user to make certain they really want to close the browser window...if they confirm the close, the window closes, but if they click no, the window remains...


- spewn

 
I tried:

<body onbeforeunload=&quot;confirm('You Sure!')&quot;>

and it still closes the window. Any other ideas?
 
spewn you were right I was using unbeforeunload incorrectly.

The following works like a charm. Some people shouldn't be so &quot;Quick&quot; to answer! :)

<SCRIPT LANGUAGE=&quot;JavaScript1.2&quot; TYPE=&quot;text/javascript&quot;>
<!--
window.onbeforeunload = unloadAnnounce;
function unloadAnnounce(){
Announce = &quot;You sure you want to close?&quot;
return Announce;
}
//-->
</SCRIPT>
 
I'm not a javascript person so this might be a dumb question. But I'd appreciate any help.

Can the OK (to continue closing) button be removed? I have an application that it is imperative that users log out before closing the browser.

I just want an Alert that tells them to log out before closing.

Thank you for your patience and help.

Ken
 

you know, you could throw the confirm pop up, and have it prompt the user like...

are you sure you want to logout?

and if the confirm, then on 'yes' click, run a function to log them out, then close the window...

once again i can't test this out as my comp box doesn't have the notepad.exe or any text editor...but if trouble persists, someone should be able to assist, or i'll check it out once i get home...


- spewn
 
I am trying to implement this above and it works except that whenever I try to go to a diffrent page within the application it is giving me the alert box. I have placed the java in a header that is included on all pages and I did not need to put anything in the body tag to get it to work. Any ideas?
 
bcdeveloper
no you can not remove the OK or any button for that matter.

thysonj
post the code! You cannot mandate productivity, you must provide the tools to let people become their best.
-Steve Jobs
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top