需求:日常開發(fā)中,網(wǎng)站需求是用戶點(diǎn)擊瀏覽器的返回事件出吹,網(wǎng)站會(huì)執(zhí)行某些操作。
先來(lái)看看效果:
代碼一:
$(function(){
pushHistory();
window.addEventListener("popstate", function(e) {
alert("監(jiān)聽到返回按鈕事件啦");
//根據(jù)自己的需求實(shí)現(xiàn)自己的功能
//window.location.
? ? ? ? },false);
function pushHistory() {
var state = {
title:"title",
url:"#"
};
window.history.pushState(state,"title","#");
}
});
代碼二:
$(document).ready(function (e) {
var counter =0;
if (window.history && window.history.pushState) {
$(window).on('popstate', function () {
window.history.pushState('forward',null,'#');
window.history.forward(1);
// alert("不可回退");? //如果需在彈框就有它
? ? ? ? ? ? ? ? self.location="orderinfo.html";//如查需要跳轉(zhuǎn)頁(yè)面就用它
? ? ? ? ? ? });
}
window.history.pushState('forward',null,'#');//在IE中必須得有這兩行
? ? ? ? window.history.forward(1);
});