js 监听返回键

js
window.onload = function(){
    window.addEventListener('popstate', function (e) {
        alert('监听到了返回');
    });
    window.history.pushState('forward', null, '#'); //在IE中必须得有这两行
    window.history.forward(1);
}

jq
$(document).ready(function(e) {
    var counter = 0;
    if (window.history && window.history.pushState) {
        $(window).on('popstate', function () {
            alert('监听到了返回');
        });
    }
    window.history.pushState('forward', null, '#'); //在IE中必须得有这两行
    window.history.forward(1);
});
posted @ 2022-07-20 18:15  猫老板的豆  阅读(439)  评论(0编辑  收藏  举报