9_历史管理history.html 775 B

1234567891011121314151617181920212223242526272829
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <button>添加历史记录</button>
  10. <script>
  11. var oBtn = document.querySelector("button");
  12. var num = 0;
  13. oBtn.onclick = function(){
  14. // 添加历史记录
  15. num++;
  16. history.pushState("h1"+num,"");
  17. }
  18. // 监听history变化
  19. window.onpopstate = function(e){
  20. console.log("history变化了",e.state);
  21. console.log(history.state);
  22. }
  23. // hash 和 history 区别
  24. // hash 是url的片段
  25. // history 是浏览器的历史记录
  26. </script>
  27. </body>
  28. </html>