| 1234567891011121314151617181920212223242526272829303132333435 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <!-- 历史管理 history -->
- <button id="btn">点击我</button>
- <script>
- // 获取元素
- var btn = document.getElementById("btn");
- // 绑定事件
- btn.onclick = function () {
- // 点击按钮后,改变历史记录
- // history中通过pushState方法添加历史记录
- // pushState 有三个值 第一个参数是添加的值 第二个参数是标题(不能省略一帮为空字符串) 第三个参数是url
- // history.pushState(val,title,url);
- var ran = Math.random();
- history.pushState(ran,"");
- }
- // 监听历史记录变化 onpopstate
- window.onpopstate = function () {
- console.log(history.state);
- }
- // history 与 hash 区别
- // history不会改变url,hash会改变url
- </script>
- </body>
- </html>
|