可以解决什么问题:

可以实现网页的ajax加载,同时又能完成URL的改变而没有网页跳转刷新的迹象,就像是改变了网页的hash(#)一样。

优于hash:

避免了改变hash的问题,避免了用户不理解URL的形式感到疑惑,同时还有onpopstate提供监听,良好响应后退前进。而且它不需要这个URL真实存在。

HTML5 的 pushState+Ajax:

pushState() 的基本参数是:

 var state = ( {
url: ~href, title: ~title, ~additionalKEY: ~additionalVALUE
} );
window.history.pushState(state, ~title, ~href);
window.history.replaceState( state, ~title, ~href); // 替换

pushState、replaceState 的区别

pushState()可以创建历史,可以配合popstate事件,而replaceState()则是替换掉当前的URL,不会产生历史。

注:

只能用同域的URL替换,例如你不能用http://baidu.com去替换http://google.com。

state对象不存储不可序列化的对象如DOM。

pushState 配合 popstate 监听

 window.addEventListener('popstate', function(evt){
var state = evt.state;
var tilte = state.tilte ;
var url = state.url
document.title=title;
doSomething(url); // 自定义函数
}, false);

popstate与hashChange

对于http://www.abc.com/path?search=ghj#hashstring 这个url来说,

① http://为协议
② www.abc.com为host
③ path为path,也就是我们的子目录,每个子目录可以干很多事情
④ #hashstring为hash相关

①-③页面会刷新,④页面不刷新,

pushState与改变hash 相同点:可以改变url地址

pushState与改变hash 不同点:hashChange只能改变hashstring 部分,pushState可改变①-③而不刷新页面

popstate 为1-3环节的变化引起的回调
hashChange 为hash变化引起的回调,是不同的

实例应用 - 劫持后退按钮

(function(window, location) {
history.replaceState(null, document.title, location.pathname+"#!/stealingyourhistory");
history.pushState(null, document.title, location.pathname);
window.addEventListener("popstate", function() {
if(location.hash === "#!/stealingyourhistory") {
history.replaceState(null, document.title, location.pathname);
setTimeout(function(){
location.replace("http://www.baidu.com/");
},0); } }, false); }(window, location));

(一)HTML5 - pushState 无刷新更新地址的更多相关文章

  1. HTML5 API——无刷新更新地址 history.pushState/replaceState 方法

    尽 管是上面讲到的<JavaScript高级程序设计>(第二版)中提到,BOM中的location.path/query…… (window.location)在通过JavaScript更 ...

  2. 【转】HTML5 API——无刷新更新地址 history.pushState/replaceState 方法

    (window.location)在通过JavaScript更改以后,浏览器都会通过刷新来到达你更改后的URL(location的意思就是位 置..) 而在JavaScript MVC开始流行之后,通 ...

  3. HTML5 API—无刷新更新地址 history.pushState/replaceState方法(例子) (转)

    尽管是上面讲到的<JavaScript高级程序设计>(第二版)中提到,BOM中的location.path/query…… (window.location)在通过JavaScript更改 ...

  4. window.history.pushState与ajax实现无刷新更新页面url

    ajax能无刷新更新数据,但是不能更新url HTML5的新API: window.history.pushState, window.history.replaceState 用户操作history ...

  5. 通过history.pushState无刷新改变url

    通过history.pushState无刷新改变url 背景 在浏览器中改变地址栏url,将会触发页面资源的重新加载,这使得我们可以在不同的页面间进行跳转,得以浏览不同的内容.但随着单页应用的增多,越 ...

  6. 使用ajax和history.pushState无刷新改变页面URL onpopstate(转)

    Javascript代码 var htmlData1 = $.ajax(    {    url: "/getXXXResponse",    async: false }).re ...

  7. history.pushState无刷新改变url

    通过history.pushState无刷新改变url 背景 在浏览器中改变地址栏url,将会触发页面资源的重新加载,这使得我们可以在不同的页面间进行跳转,得以浏览不同的内容.但随着单页应用的增多,越 ...

  8. [转]jquery 点击表格变为input可以修改无刷新更新数据

    原文地址:http://www.freejs.net/article_biaodan_43.html 之前已经发了2篇类似的文章<点击变td为input更新>和<jquery表格可编 ...

  9. jquery表格可编辑修改表格里面的值,点击td变input无刷新更新表格

    td点击后变为input可以输入,更新数据,无刷新更新 演示 XML/HTML Code <table border="0" cellpadding="0" ...

随机推荐

  1. js实现数组内元素随机排序

    其实蛮容易实现的,关键是简洁与否,下面是我自己写的. function randomSort(a){ var arr = a, random = [], len = arr.length; for ( ...

  2. js获取对象位置的方法

    scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最 ...

  3. Array and its point.

    a is the array name. &a is the ponit of 2-D array which contains a[5]. the type of &a should ...

  4. 关于Cococs中的CCActionEase

    尊重作者劳动,转载时请标明文章出处.作者:Bugs Bunny地址:http://www.cnblogs.com/cocos2d-x/archive/2012/03/13/2393898.html 本 ...

  5. Serializable序列化

    对象的输入输出流 package file; import java.io.File; import java.io.FileInputStream; import java.io.FileOutpu ...

  6. libCurl的C++引用

    I believe I have gotten LibCurl to work with Visual Studio 2013 now. Persistence ftw! Although, afte ...

  7. hibernate 映射<四>多对一双向映射

    学生和班级的例子来说,多个学生可以对应一个班级. 1.站在学生角度来说,学生就是多端.那么我们可以通过多对一来维护2者的关系. 2.站在班级的角度来说,班级就是一端.那么我们可以通过一对多啦维护2者的 ...

  8. mysql System Tablespace

    System Tablespace 数据文件配置: mysql> show variables like '%innodb_data_file_path%'; +---------------- ...

  9. NOI2010 航空管制

    http://www.lydsy.com/JudgeOnline/problem.php?id=2535 贪心. 对于第1个问,我们先建立拓扑图,对于如果a必须在b前起飞,那么连有向边b->a, ...

  10. Android通过长按图片设置为壁纸

    用imageview控件为他添加长按事件: imageview.setOnLongClickListener(new OnLongClickListener(){ public boolean onL ...