From out of curiosity can i Control window.onbeforeunload event like check if the user decided to leave the page or stay in it and can I raise an alert or some function based on
Put a timer of one second. Clear it on unload. Should work, but didn't test it.
window.addEventListener('beforeunload', function (event) {
var timeId = setTimeout(yourFunctionIfTheUserStays, 1000);
window.addEventListener('unload', function (event) {
clearTimeout(timeId);
})
return 'Are you sure you want to leave this page?';
});