js window.history.pushState()_replaceState()_popstate事件.txt 参考:https://blog.csdn.net/m0_37999242/article/details/79415055 1.window.history对象,用来保存浏览历史。 如果当前窗口先后访问了三个网址,那么history对象就包括三项。 history.length // 3 2.history对象提供了一系列方法,允许在浏览历史之间移动。 back():移动到上一个访问页面,等同于浏览器的后退键。 forward():移动到下一个访问页面,等同于浏览器的前进键。 go():接受一个整数作为参数,移动到该整数指定的页面, 比如go(1)相当于forward(),go(-1)相当于back()。 history.back(); history.forward(); history.go(-2); 如果移动的位置超出了访问历史的边界,以上三个方法并不报错,而是默默的失败。 history.go(0);相当于刷新当前页面。 常见的“返回上一页”链接,代码如下。 document.getElementById('backLink').onclick = function () { window.history.back(); } 注意:返回上一页时,页面通常是从浏览器缓存之中加载,而不是重新要求服务器发送新的网页。 3.history.pushState() HTML5为history对象添加了两个新方法, history.pushState()和 history.replaceState(), 用来在浏览历史中添加和修改记录。 if (!!(window.history && history.pushState)){ // 支持History API } else { // 不支持 } 上面的代码可以用来检查,当前浏览器是否支持History API。 如果不支持的话,可以考虑使用Polyfill库History.js。 4.history.pushState方法接受三个参数,依次为: state:一个与指定网址相关的状态对象,popstate事件触发时,该对象会传入回调函数。 如果不需要这个对象,此处可以填null。 title:新页面的标题,但是所有浏览器目前都忽略这个值,因此这里可以填null。 url:新的网址,必须与当前页面处在同一个域。浏览器的地址栏将显示这个网址。 示例: 假定当前网址是https://hao.360.com/, 我们使用pushState方法在浏览记录(history对象)中添加一个新记录。 let stateObj = { foo: 'bar' }; history.pushState(stateObj, 'page 2', '2.html'); 添加上面这个新记录后,浏览器地址栏立刻显示https://hao.360.com/2.html, 但并不会跳转到2.html,甚至也不会检查2.html是否存在, 它只是成为浏览历史中的最新记录。 假定这时你访问了https://www.baidu.com/,然后点击了倒退按钮, 页面的url将显示https://hao.360.com/2.html,但是会刷新404 Not Found。 你再点击一次倒退按钮,url将显示https://hao.360.com/,内容404 Not Found。 再点击刷新会正常。 总之,pushState方法不会触发页面刷新,只是导致history对象发生变化,地址栏会有反应。 如果pushState的url参数,设置了一个新的锚点值(即hash),并不会触发hashchange事件。 如果设置了一个跨域网址,则会报错。 // 报错 history.pushState(null, null, 'https://twitter.com/hello'); 上面代码中,pushState想要插入一个跨域的网址,导致报错。 这样设计的目的是,防止恶意代码让用户以为他们是在另一个网站上。 5.history.replaceState() history.replaceState方法的参数与pushState方法一模一样, 区别是它修改浏览历史中的当前纪录。 假定当前网页是http://example.com/example.html。 history.pushState({page: 1}, 'title 1', '?page=1'); history.pushState({page: 2}, 'title 2', '?page=2'); history.replaceState({page: 3}, 'title 3', '?page=3'); history.back(); // url显示为http://example.com/example.html?page=1 history.back(); // url显示为http://example.com/example.html history.go(2); // url显示为http://example.com/example.html?page=3 6.history.state属性 history.state属性返回当前页面的state对象。 history.pushState({page: 1}, 'title 1', '?page=1'); history.state // { page: 1 } 7.popstate事件 每当同一个文档的浏览历史(即history对象)出现变化时,就会触发popstate事件。 需要注意的是, 仅仅调用pushState方法或replaceState方法,并不会触发该事件, 只有用户点击浏览器倒退按钮和前进按钮,或者使用JavaScript调用back、forward、go方法时才会触发。 另外,该事件只针对同一个文档,如果浏览历史的切换,导致加载不同的文档,该事件也不会触发。 使用的时候,可以为popstate事件指定回调函数。 这个回调函数的参数是一个event事件对象, 它的state属性指向pushState和replaceState方法为当前URL绑定的state对象(即这两个方法的第一个参数)。 window.onpopstate = function (event) { console.log('location: ' + document.location); console.log('state: ' + JSON.stringify(event.state)); }; // 或者 window.addEventListener('popstate', function(event) { console.log('location: ' + document.location); console.log('state: ' + JSON.stringify(event.state)); }); 这个state对象也可以直接通过history对象读取。 let currentState = history.state; 注意: 页面第一次加载的时候,在load事件发生后, Chrome和Safari浏览器(Webkit核心)会触发popstate事件, 而Firefox和IE浏览器不会。
js window.history.pushState()_replaceState()_popstate事件.txt
最新推荐文章于 2024-04-25 00:32:22 发布