使用history.pushState()和popstate事件实现AJAX的前进、后退功能
来源:互联网 发布:土耳其语 知乎 编辑:程序博客网 时间:2024/06/05 00:37
上一篇文章中,我们使用location.hash来模拟ajax的前进后退功能。使用location.hash存在下面几个问题:
1.使用location.hash会导致地址栏的url发生变化,用户体验不够友好。
2.location.hash产生的历史记录无法修改,每次hash改变都会导致产生一个新的历史记录。
3.location.hash只是1个字符串,不能存储很多状态相关的信息。
为了解决这些问题,HTML5中引入了history.pushState()、history.replaceState()、popstate事件来处理浏览器历史记录的问题。下面的代码可以达到跟location.hash相同的效果,可以看到地址栏url不会改变。
<!DOCTYPE html> <html><head><meta charset="utf-8"><script type="text/javascript" src="jquery-1.11.1.min.js"></script><script type="text/javascript">var currentPageIndex = 0;window.onload = function(){currentPageIndex = 0;showPageAtIndex(currentPageIndex);addHistory(currentPageIndex);}// onpopstate可以监控state变化window.onpopstate = function(e){if(history.state){var state = history.state;showPageAtIndex(state.id);}}function toNextPageWhenClick(){currentPageIndex++;if(isValidPageIndex(currentPageIndex)){showPageAtIndex(currentPageIndex);addHistory(currentPageIndex);}else{return;}}function showPageAtIndex(id){$("div[id!="+id+"]").hide();$("#"+id).show();if(isHomePage(id)){$("input").attr("value","current is home page,next page=1");}else if(isLastPage(id)){$("input").attr("value","current page="+id+", it is the end.");}else{$("input").attr("value","current page="+id+",next page="+(id+1));}}function isValidPageIndex(id){return id <= 5;}function isLastPage(id){return id == 5;}function isHomePage(id){return id == 0;}// 增加历史记录function addHistory(id){history.pushState({"id":id},"","");}</script><style>.navigate{height:100px;width:300px;background-color:#0000ff;display:none;}.home{height:100px;width:300px;background-color:#00ff00;display:none;}.last{height:100px;width:300px;background-color:#ff0000;display:none;}</style></head> <body><input type="button" value="" onclick="toNextPageWhenClick();"><div class="home" id="0">HOME PAGE</div><div class="navigate" id="1">ajax1</div><div class="navigate" id="2">ajax2</div><div class="navigate" id="3">ajax3</div><div class="navigate" id="4">ajax4</div><div class="last" id="5">last page</div></body></html>
0 0
- 使用history.pushState()和popstate事件实现AJAX的前进、后退功能
- pushState()、popstate事件配合ajax实现浏览器前进后退页面局部刷新
- 使用ajax和history.pushState无刷新改变页面URL(ajax中前进后退)
- html5新特性:利用history的pushState等方法来解决使用ajax导致页面后退和前进的问题
- history pushState 实现浏览器前进与后退
- JavaScript Window History 实现浏览器的 前进 后退 功能
- ajax 的前进 后退 问题 jquery.history
- 几种方法实现ajax请求内容时使用浏览器后退和前进功能
- 几种方法实现ajax请求内容时使用浏览器后退和前进功能
- 使用hash来实现Ajax前进后退
- 使用Ajax和history.pushState()实现无刷新改变页面内容和地址栏Url
- AJAX 前进后退 (RSH--Really Simple History)
- JavaScript的history对象实现页面前进、后退、跳转
- AJAX的刷新和前进后退问题解决
- AJAX的刷新和前进后退问题解决
- AJAX的刷新和前进后退问题解决
- AJAX的刷新和前进后退问题解决
- UIWebView的前进和后退功能
- Java-->使用netty搭建Http服务器
- 第一步:写一个带进度条的下载文件的小Demo
- ios 方向工具汇总记录 实时补充
- Linux下开启/关闭MySql Server命令
- LeetCode 之 Length of Last Word — C 实现
- 使用history.pushState()和popstate事件实现AJAX的前进、后退功能
- org.apache.log4j.Logger详解
- apt-get install xxx Segmentation fault (core dumped)
- android MediaRecorder录音
- nyoj-19-擅长排列的小明
- iOS 的 XMPPFramework 简介
- 减肥食谱之——我的带饭策略: www.haidi88.com
- 使用 GNU Libtool 创建库
- vs2008转为2013之后,在对话框上添加控件程序直接退出