I have implemented some function when the browser will be closed.
window.onbeforeunload = function (evt) {
evt = (evt)? evt:event;
clickY = evt.clie
window.onbeforeunload event is calls before your page is unloaded. There is no method to check if it's refresh or browser window close event.
It fires by different scenarios MSDN:
anchor.click method.document.write method.document.close method.window.close method.window.navigate or NavigateAndFind method.location.replace method.location.reload method.location.href property.type=submit control, or invoke the form.submit method.window.open method, providing the possible value _self for the window name.document.open method.And you can't handle this.
Also note that window.onbeforeunload event supposed to inform user that he leaves page and handler function should return string value that will be included in confirmation popup (except Firefox 4+ see bug).
You can not force the user to stay on the page.
Note: The only thing you can do with onbeforeunload is to notify the user that he is leaving your page
(this is because some evil pages can open another pages and so on... and spam users. This is security restriction).
If you want to make AJAX call before window is unloaded you need to use onunload event. Note that your AJAX call must me synchronus. Asynchronous call will not work.