How to raise JS event when user shy away from the present page

our requirement is client wants to know howmany users leaving the main page without entering
data in the application that time we need to show the css popup overlay.

in the following scenarios overlay should pop up.

1)when user leaves the page.(e.g. type other address in the address bar).bascially user shy away from the browser.
2)clicks browser close button.

we will show one css popup overlay(not browser popup) and in the back ground main application will be greyed out.this overlay consists of some text fields and some buttons will be there and parallely in the back ground we will execute AJAX call.

can we accomplish this requirement??is yes how?will it work in all browsers?my concern is how to prevent the browser from closing when css overlay comes up?

The only scripting that exists is to determine whether the page is about to be “unloaded”. That means they might have clicked on one of your links, or they close the page, or navigate to a different web site, or submit your form. Determining if the page is about to be unloaded only works in some web browsers.

In terms of security, web browsers do not allow you to lock people in. There are though modal dialog-boxes, where people cannot do anything with your current page, until they deal with the dialog.

Thanks for the the main form we have multiple buttons.if we click one of the button at that time only we need to show this you said when the page is “unloaded” i think we can call “onbeforeunload” event ,In that event can we call this modal dialog box?

i think whenever you call this event “onbeforeunload” i am afraid that whatever you click(any buttons,links etc…) in the main page then this overlay will come.but we should have the capabitlity when to show this overlay.

we should call this overlay in the below scenarios only.

1)close the page, or navigate to a different web site.
2)click one of the button in the form.
3)when session times out .(if we make it works above scenarios.i think we can do this as well.)

Would it be a problem if the onbeforeunload event is not capable of working in some web browsers?

From an IE article about onbeforeunload, the activities that cause the onbeforeunload to occur are:

[list][]Close the current window.
]Navigate to another location by entering a new address or selecting a Favorite.
[]Click an anchor that refers to another document.
]Invoke the method.
[]Invoke the document.write method.
]Invoke the document.close method.
[]Invoke the window.close method.
]Invoke the window.navigate or NavigateAndFind method.
[]Invoke the location.replace method.
]Invoke the location.reload method.
[]Specify a new value for the location.href property.
]Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
[]Invoke the method, providing the possible value _self for the window name.
]Invoke the method.
[*]Click the Back, Forward, Refresh, or Home button.[/list]