首页 > 技术文章 > 监听浏览器返回上一页

zhengyan 2017-05-27 13:40 原文

页面1 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="2.html">点击2</a>
</body>
</html>

页面2

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>

window.addEventListener("popstate", function() {
  var currentState = history.state;
  window.location.href="3.html";
});

$(document).ready(function(){
  if("onhashchange" in window){
    var json={time:new Date().getTime()};
    window.history.pushState(json,'','3.html');
    console.log(window.history)
  }
});
</script>
</head>
<body>
<p>我是第二个页面</p>
</body>
</html>

 

从页面1到页面2 

 

推荐阅读