I need to use onbeforeunload everywhere, when user leave my site.
var leave_message = 'Leave message';
var validNavigation;
function goodbye(e) {
if (!validNavigation) {
if (!e) e = window.event;
e.cancelBubble = true;
e.returnValue = leave_message;
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
return leave_message;
}
}
$("body").on('click', 'a', function () {
validNavigation = true;
});
window.onbeforeunload = goodbye;
But there is many places in my javascript, where i use window.location for redirecting user to internal pages. And i tired of pasting validNavigation = true; in these parts of code.
Please give me advice how i can setup it globally?