aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Using onbeforeunload event of Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Using onbeforeunload event of Javascript" Watch "Using onbeforeunload event of Javascript" New topic
Author

Using onbeforeunload event of Javascript

Lavanya Mothilal
Ranch Hand

Joined: Dec 27, 2007
Posts: 30
Hi all,

I have been using the following script to ask the user if they wanted to close the window.

<script language="JavaScript" event="onbeforeunload()" for="window">
event.returnValue = "Are you sure you want to close this application ? You have unsaved data and closing this application will cause any unsaved data to be lost. Do you wish to continue closing the application ?";
</script>

This message is triggered whenever the page unloads. But I want to display the above message only when the user clicks the X mark in the window. Is there any way through which i can achieve this?

I tried to find out if the user has clicked the X mark by getting the coordinate of the document using the following code .

<script language="JavaScript" event="onbeforeunload()" for="window">
var coordX = event.clientX + document.body.scrollLeft;
var coordY = event.clientY + document.body.scrollTop;
if ((coordX > 770 && coordY < -110){
event.returnValue = "Are you sure you want to
close this application ? You have unsaved data and closing this application will cause any unsaved data to be lost. Do you wish to continue closing the application ?";}
</script>

But the problem is it doesnt work for all resolutions. Also,When a list box grows up to the title bar, then selecting any value from the list box triggers this message.

Can anyone suggest me some idea ?


Thanks,<br />Lavanya
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The detect cursor position X hack only works in one browser and it does not account for alt f4, ctrl-w, and file-quit to name a few.

Also, there are better ways of adding script to the page that using events on script tags.

The best way to code for unbeforeunload is to not show it based on events that you want to happen. You set a flag submission on links, the form submit, and so on.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using onbeforeunload event of Javascript