Here is working solution for Firefox and Chrome. I haven’t yet tested in Safari and Opera.
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
var confirmationMessage="Are you sure to leave the page?";
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});