使用PHP或Javascript检测页面刷新或新加载



如何在使用PHP或Javascript的浏览器中检测选项卡关闭。换句话说,如何查找页面是否在新选项卡中刷新或打开。我关心的是选项卡,而不是浏览器。

您可以为window.onbeforeunload事件设置一个侦听器。不过,您将无法检测到该选项卡是否已通过JavaScript关闭。

实际上,Javascript可以告诉您是否要关闭Tab。需要使用两种不同的方法(一种用于IE,另一种用于其他人)。

我编写了一个Javascript流程来满足您的要求。这个过程的先决条件是jQuery和一个插件(livequery,因为有些HTML是在页面加载后动态生成的)。不管怎样,这里是执行这一切的js文件(checkclosing.js):

// Global Var
var bodyclicked = false;
var datachanged = false;
var nodirtycheck = false;
// Start the engines :)
$(document).ready(function () {
    init();
});
function init() {
    bindEvents();
}
function bindEvents() {
    // Bind the onClick event for the DOM body
    $(document).livequery(function () {
        bodyclicked = true;
    });
    // Bind our event handlers for the change and reset events
    $(':input').not('.excludeFromDirtyCheck').bind('change', function () {
        if ($(this).hasClass('dataLoader')) {
            if (!datachanged) {
                return;
            }
        }
        datachanged = true;
        $(".hidDataChanged").val("True");
    });
    $(':reset,:submit').bind('click', function () {
        // .NET renders some ASP Buttons as Submit and Reset types
        if ($(this).hasClass('notSubmit')) {
            return;
        }
        if ($(this).hasClass('notReset')) {
            return;
        }
        datachanged = false;
        $(".hidDataChanged").val("False");
    });
    // Must have the livequery plugin referenced for this to work
    $('.resetchangedform').livequery('click', function (event) {
        //alert("resetchanged"); // FIXME
        datachanged = false;
        // Set our hidden input (on the Administration Master page)
        $(".hidDataChanged").val("False");
    });
    // Must have the livequery plugin referenced for this to work
    $('.setchangedform').livequery('click', function (event) {
        //alert("setchanged"); // FIXME
        datachanged = true;
        // Set our hidden input (on the Administration Master page)
        $(".hidDataChanged").val("True");
    });
    // Must have the livequery plugin referenced for this to work
    $('.excludeFromDirtyCheck').livequery('click', function (event) {
        nodirtycheck = true;
    });
    // Must have the livequery plugin referenced for this to work
    $('.notSubmit').livequery('click', function (event) {
        nodirtycheck = true;
    });
    // Must have the livequery plugin referenced for this to work
    $('.dataReloader').livequery('change', function (event) { 
        nodirtycheck = true;
    });
    window.onbeforeunload = function () {
       //alert("datachanged = " + datachanged + " | nodirtycheck = " + nodirtycheck + " | hidDataChanged = " + $(".hidDataChanged").val());
        // Check the hidden textbox
        if ($(".hidDataChanged").val() == "True") {
            datachanged = true;
        }
        if (nodirtycheck) {
            nodirtycheck = false;
        }
        else {
            if (navigator.appName == "Microsoft Internet Explorer") {
                // IE
                if (bodyclicked) {
                    if (datachanged) {
                        return "You may have unsaved changes...";
                    }
                }
                else {
                    bodyclicked = false;
                    // Do Nothing
                }
            }
            else {
                // Not IE
                if (datachanged) {
                    return "You may have unsaved changes...";
                }
                else {
                    // Do Nothing
                }
            } //if (navigator.appName == "Microsoft Internet Explorer") {
        } //window.onbeforeunload = function () {
    }     // if (nodirtycheck) {
}

然后,只需在任何你想检查标签关闭的页面上包含对该文件的引用:

这是为了防止用户从表单值未保存更改的页面中导航出来。是的,我知道大多数时候,阻止用户关闭选项卡或导航离开是不好的做法,但在这种情况下,用户请求了它,这是一个不供公众使用的内部应用程序。

在让用户关闭选项卡或导航离开之前,任何不想检查更改的控件都将被赋予excludeFromDirtyCheck的类名。

这可能比你需要的要多,但你可以去掉无用的部分。基本前提是一样的。

最新更新