onunload (or onbeforeunload ) cannot redirect the user to another page. This is for security reasons.
If you want to show the invitation before the user leaves the page, use onbeforeunload:
window.onbeforeunload = function(){ return 'Are you sure you want to leave?'; };
Or using jQuery:
$(window).bind('beforeunload', function(){ return 'Are you sure you want to leave?'; });
It will simply ask the user if they want to leave the page or not, you cannot redirect them if they want to stay on the page. If they leave, the browser will go where they told him to leave.
You can use onunload to perform operations before the page is unloaded, but you cannot redirect from there (Chrome 14+ blocks warnings inside onunload):
window.onunload = function() { alert('Bye.'); }
Or using jQuery:
$(window).unload(function(){ alert('Bye.'); });
Jitendra pancholi
source share