Yes, there is a way. The onbeforeunload function works a little differently than other events. All you have to do is return a string from this function, and the browser will do all the work for you. The syntax is as follows:
window.onbeforeunload = function () {
return "Close?";
}
And all you have to do. When you click "Cancel" in the dialog box that appears, the user will be on the current page, and OK will allow the user to go or close the page. It is really simple enough that you do not need to use jQuery at all.
Chibu source
share