0

The following works in Chrome but not in Edge/IE. document.activeElement.href always returns "undefined" for Edge/IE. Is there a way to determine if destination URL is for my site or a external site?

window.addEventListener("beforeunload", function (e) {
        if (document.activeElement.href == undefined) {
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "/Profile/DestroySession",
                dataType: "json",
                async: true,
                success: function (data) {
                    alert(data.d);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(textStatus);
                    console.log(errorThrown);
                    debugger;
                }
            });
        }
    });
Kippes
  • 1
  • It seems that it is not possible, take a look at: https://stackoverflow.com/questions/1686687/how-can-i-get-the-destination-url-for-the-onbeforeunload-event – Lety Sep 16 '20 at 16:36

1 Answers1

1

I would suggest to use the form.OnUnload event. When users redirect to another website, the form.OnUnload event fires. When this happens, you can execute some javascript code to clear out the session variable. For that, you need to create XMLHTTP object and call session.Abandon() internally. Hope this tutorial can help you: http://codinginparadise.org/weblog/2005/08/ajax-tutorial-saving-session-across.html

Rute Lemos
  • 191
  • 1
  • 11