您现在的位置是:首页 > 其他

李清波 2020-06-04 其他 1523 复制当前网址

JS监听手机返回键,回到指定界面


方法一:

$(function(){ 
pushHistory(); 
window.addEventListener(“popstate”, function(e) { 
window.location = 返回的地址 
}, false); 
function pushHistory() { 
var state = { 
title: “title”, 
url: “#” 
}; 
window.history.pushState(state, “title”, “#”); 
} 
});

方法二:

if(window.history && window.history.pushState) {
    $(window).on('popstate', function() {
        var hashLocation = location.hash;
        var hashSplit = hashLocation.split("#!/");
        var hashName = hashSplit[1];
        if(hashName !== '') {
            var hash = window.location.hash;
            if(hash === '') {
                alert("你点击了返回键");
            }
        }
    });
    window.history.pushState('forward', null, './#forward');
}

方法三:

pushHistory(); 

window.addEventListener("popstate", function(e) { 
    window.location = 'http://www.baidu.com';
}, false); 

function pushHistory() { 
    var state = { 
        title: "title", 
        url: "#"
    }; 
    window.history.pushState(state, "title", "#"); 
}



文章来源:https://www.liqingbo.com/blog-1716.html

评论