How does one determine what is the trigger of an event (close browser, close tab, redirect on other page etc.)?
function winUnload(){
var pathToCloseCurrentTab = window.location.protocol + "//" + window.location.host + '<%= ResolveUrl("~/Services/SenderAjax.asmx") %>' + "/CloseCurrentTab";
$.ajax({
type: 'POST',
url: pathToCloseCurrentTab,
dataType: 'JSON',
data: {'data': $('input[id$=hfTimeLoadLayOut]').val()}
});
}
window.onbeforeunload= function (evt){
winUnload();
};
You can’t determine what triggered the unload event – the event target will be the document itself, and relying on different states of elements is not reliable as there are plenty of actions that can trigger it.
Though, if you only need to know if a certain button, link or form (and etc.) triggered it, you can listen on their
click/submitevent thus catching the unload event before it has happened.