检测返回/下一步/刷新按钮点击Chrome

时间:2014-08-26 16:28:41

标签: javascript jquery google-chrome

如何检测返回/下一个/刷新按钮点击Chrome?

我已经有了键盘检测解决方案,但它不适用于浏览器中的按钮。

<script language="javascript" >
var validNavigation = false;
var numPendencias = 0;

function verificaPendencias() {

    var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
    var leave_message = 'Existem pendências ativas, deseja mesmo sair?'
    function goodbye(e) {
        if (!validNavigation) {
            if (dont_confirm_leave!==1) {
                if(!e) e = window.event;
                //e.cancelBubble is supported by IE - this will kill the bubbling process.
                e.cancelBubble = true;
                e.returnValue = leave_message;
                //e.stopPropagation works in Firefox.
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }                    
                //return works for Chrome and Safari
                return leave_message;
            }
        }
    }

    window.onbeforeunload=goodbye;

}

function atribuirEventos() {    
    jQuery(document).bind('keypress', function(e) {
        if (e.keyCode == 116){
          validNavigation = true;
        }
    });

    jQuery(document).bind('keydown', function(e) {
        if (e.keyCode == 116){
          validNavigation = true;
        }
    });

    jQuery(document).bind('keyup', function(e) {
        if (e.keyCode == 116){
          validNavigation = true;
        }
    });

    jQuery("a").bind("mouseover", function() {
        validNavigation = true;
    });                
    jQuery("a").bind("mouseout", function() {
        validNavigation = false;
    });

    jQuery("form").bind("submit", function() {
        validNavigation = true;
    });

    jQuery("input[type=submit]").bind("click", function() {
        validNavigation = true;
    });


    jQuery("button").bind("mouseover", function() {
        validNavigation = true;            

    });                
    jQuery("button").bind("mouseout", function() {
        validNavigation = false;

    });        
}


jQuery(document).ready(function() {
    verificaPendencias();
    atribuirEventos();
});    

是否有一些显示此事件的属性?

为了更准确,我想在用户关闭浏览器或标签时生成警告,但问题是通过onbeforeunload,每次他点击一个链接,按下F5或做任何出来的事情页面本身它显示消息,然后我把锁,但即使如此,当用户点击浏览器的按钮它生成消息时,我想避免使用上面的方法并仅在翻盖关闭时留言或浏览器。

1 个答案:

答案 0 :(得分:0)

我认为你需要使用类似的东西:

window.onpopstate = function(event) {
    // do something  
};