2

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();
};
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
Max
  • 417
  • 7
  • 21
  • Duplicate of http://stackoverflow.com/q/3757942/615754? I think the answer is that you can't determine what caused it. – nnnnnn Jun 15 '12 at 11:24

1 Answers1

0

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/submit event thus catching the unload event before it has happened.

Dvir
  • 5,049
  • 1
  • 23
  • 32