用户离开我的网站时如何注销?

问题描述

以下内容适用于Chrome,但不适用于Edge / IE。 document.activeElement.href始终为Edge / IE返回“未定义”。有没有办法确定目标URL是用于我的网站还是外部网站?

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;
                }
            });
        }
    });

解决方法

我建议使用form.OnUnload事件。当用户重定向到另一个网站时,将触发form.OnUnload事件。 发生这种情况时,您可以执行一些javascript代码来清除会话变量。为此,您需要创建XMLHTTP对象并在内部调用session.Abandon()。希望本教程可以为您提供帮助:http://codinginparadise.org/weblog/2005/08/ajax-tutorial-saving-session-across.html