// 替换当前浏览器history的最后一项记录。
history.replaceState([data], [title], [url]);
//在当前history的中,添加一项记录。
history.pushState([data], [title], [url]);
//1). In Firefox data is limited to 640k characters.
//2). 参数 title 为字符串,当前浏览器一般忽略它。
//3). url 默认是相对当前路径。支持. / .. 路径表示。
注意: 对history的修改,是受同源策略限制的。
//example:当前页面:www.google.com.hk
history.pushState(null, null, 'hello');
www.google.com.hk ----> www.google.com.hk/hello
history.pushState(null, null, '#jack');
www.google.com.hk/hello ----> www.google.com.hk/hello#jack
history.pushState(null, null, 'http:www.baidu.com');//ERROR
//后退、前进按钮,将触发该事件。
window.addEventListener('popstate', function(e) {
// e.state is equal to the data-attribute of the last image we clicked
});
注意:在Android原生浏览器、Chrome (prior to v34) 、 Safari 上,页面第一次加载的时候,会自动触发该事件。
//历史记录变更就触发,包括:参数、hash、路径的变更。
window.onpopstate = function(event) {
alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
// 1. pushState / replaceState 不会触发该事件;
// 2. 用户的点击动作(后退、a链接)会触发事件。
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(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: http://example.com/example.html, state: null
history.go(2); // alerts "location: http://example.com/example.html?page=3, state: {"page":3}
// 参考:
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history
https://css-tricks.com/using-the-html5-history-api/
分享到:
相关推荐
connect-history-api-fallback, 返回到使用 HTML 5历史记录API的应用程序的回退到 index.html connect-history-api-fallback通过指定索引页面向代理请求请求中间件,这对于使用HTML5历史API的单页应用程序。...
前端项目-html5-history-api,不支持pushstate、replaceState的浏览器的HTML5历史API扩展
history-server:一个用于单页应用的 HTTP服务器使用HTML5 history API实现
主要介绍了深入探究HTML5的History API,重点讲述了HTML5中新的方法history.pushState()和history.replaceState(),需要的朋友可以参考下
“HistoryAPI:滚动恢复”的Polyfill_HTML_JavaScript_下载.zip
系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例,让大家一步一步的体会"h5"能够做什么,以及在实际项目中如何去合理的运用达到使用自如,完美驾驭O(∩_∩)O~,
可以完全按照例如Dive Into HTML5书( )或History API规范( )中的描述使用该库 )。 您可以使用以下命令安装此插件: npm install html5-history-api 浏览器支持: history.js -IE8 +和其他浏览器 history....
HistoryLocation是包装 HTML5 历史 API。 // ececute http://example.com/ import { HashLocation } from 'common-history' ; const hashLocation = new HashLocation ( ) ; // It is executed each time the ...
发现登陆、注册动画效果非常华丽,但让我感到震惊的是页面竟能够实现无刷新跳转,回顾了所学的前端知识,似乎没有任何技术可以实现这一点,于是百度了一下,才发现这原来是使用HTML5中History API实现的效果
2、History 对象 7 2.1、属性 7 2.2、方法 7 2.3、描述 8 3、Location 对象 8 3.1、属性 8 3.2、方法 9 4、Navigator 对象 9 4.1. Navigator 对象集合 9 4.2、Navigator 对象属性 9 4.3、Navigator 对象方法 10 4.4...
尽管也存在一些,但由于您只能使用本机HTML5 History API,因此它们相对较小。 如果您打算支持旧版浏览器,那么可以使用History.js。 该通知位于此处,因为History.js没有获得足够的资金来维护,因此它仅在旧版...
它使用的就是HTML5 history新增的几个API,作为window的一个全局变量,在HTML4的时代history已不是什么新鲜的事物了。我们经常使用的就有 history.back()以及history.go() 。 我一直以为没有办法做到,直到前两天...
HTML5 历史 API 管理器现代浏览器支持使用 JavaScript 更新浏览器 URL 并将状态数据与更新后的 URL 关联的功能。 此实用程序旨在满足本机 API 中的几个差距。 定义在调用 pushState 和触发 onpopstate 事件时执行的...
一个zepto插件,利用HTML5 History API实现的路由控制插件,适用于移动端单页面应用。 需要后台支持,当浏览器访问一个地址时,服务器返回的是这个单页面。前端路由由这个单页面来控制。 不支持hash路径控制。 提供...
* 前端需要开启history模式,而后端根据url并不知道前端在请求api还是在请求页面,如localhost:4200/home这种url,前端理所当然认为“我需要得到html,并跳转到首页”,然而后端并不能区分。 * 因此需要一种判断...
使用 html5 历史/状态 api[pop,push]。 示例: -->> 页面的 URL 是 。 --> 在 javascript 中调用这一行 historyStoreObj.push(function(){console.log("Back to homepage");},"page1"); -->> 页面的 ...
曲目Fork of 使用查询参数而不是 HTML5 History API 进行路由。 这对于使用file://协议在网站上执行路由很有用。 注意:路径存储在$path查询参数中。麻省理工学院执照版权所有 (c) 2012 Nate Smith 和 Brian Noguchi...