Closing Web Page

msvidron

Member
Joined
Jul 21, 2005
Messages
14
Programming Experience
5-10
When a user clicks the X in upper right corner of web page, to close an aspx web form, is there a way to Check some criteria on the page before allowing the page to actually close
 
Sorry. I sure that would help. Here's what I need to do. I have to listboxes on a webpage. When a user clicks on one list and it populates the second list and removes record from first list. Now, I what to check to make sure nothing is in the second list before the page closes. If the user clicks on the x in upper right corner, I need a message appear alerting them they can't close the window until they process the records in the second list.
 
I also think you have to use Javascript, first add the onbeforeunload event to body:
HTML:
<body onbeforeunload="Javascript:closeIt()">
then this script in head section:
HTML:
<script type="text/javascript">
function closeIt(){
if (document.getElementById("ListBox1").options.length>0)
{event.returnValue = "Confirmation message.";}
}
</script>
Substitute "ListBox1" for the ID of the listbox to be counted for on exit
 
JohnM,

Thanks for the reply. I tried the javascipt code you sent and still the page closes. I am getting on error on the html side saying that onbeforeunload is not an attribute of the body element. any thoughts?
 
JohnM,

I got it to work to some degree.....it shows the message, but then displays another message and allows the user to close the page. Any way to prevent this?
 
Last edited:
msvidron said:
allows the user to close the page. Any way to prevent this?
No, this is as close you get. And when you think it through, whatever is displaying in a browser window is not important if user or system needs to close that application. A confirmation that permits the close needs to be there.

My opinion is that there should be an option in supportive browsers to disable this event altogether, because of the risk of malpages to exploit it for further annoyance.
 
Back
Top