8_历史管理hash.html 978 B

12345678910111213141516171819202122232425262728293031
  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 = 1;
  13. oBtn.onclick = function(){
  14. // window.location是BOM对象下的 地址对象 可以获取当前页面的地址信息
  15. // hash 是地址栏中 # 后面的内容
  16. num++;
  17. // 向hash添加内容 添加历史记录
  18. window.location.hash = "code"+num;
  19. }
  20. // 捕捉历史记录变化事件
  21. // 当hash值发生变化时 会触发hashchange事件
  22. window.onhashchange = function(){
  23. // console.log("历史记录变化了");
  24. // 可以获取当前hash值
  25. var hash = window.location.hash;
  26. console.log(hash);
  27. }
  28. </script>
  29. </body>
  30. </html>