使用history.pushState()和popstate事件实现AJAX的前进、后退功能
上一篇文章中。我们使用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>
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWl0YW5neW9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWl0YW5neW9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
使用history.pushState()和popstate事件实现AJAX的前进、后退功能的更多相关文章
- pushState()、popstate事件配合ajax实现浏览器前进后退页面局部刷新
最近研究pushState,看了网上的文章还是不怎么会用,于是自己摸索着理解使用,终于实现局部刷新同时前进后退. 首先说说pushState(),这个函数将当前的url等信息加入history堆栈中: ...
- 监听浏览器返回,pushState,popstate 事件,window.history对象
在WebApp或浏览器中,会有点击返回.后退.上一页等按钮实现自己的关闭页面.调整到指定页面.确认离开页面或执行一些其它操作的需求.可以使用 popstate 事件进行监听返回.后退.上一页操作. 一 ...
- 关于histry的pushstate 和 popstate事件的应用
这篇文章是基础:http://www.cnblogs.com/kaituorensheng/p/3776527.html: histry的单页面应用有两个写法:哈希值和?: 哈希值例子: 实现效果:点 ...
- Ajax异步刷新地址栏url改变(利用Html5 history.pushState实现)
早些时候在博客园参阅了不少资料,然后决定入驻博客园分享自己的开发心得,最近准备转方向筹备着辞职交接工作,所以有点忙碌,搁置了一个月才匆匆写下这么一篇随笔,希望能给大家带来一点帮助吧,资料和学识有限,如 ...
- ajax与HTML5 history pushState/replaceState实例
一.本文就是个实例展示 三点: 我就TM想找个例子,知道如何个使用,使用语法什么的滚粗 跟搜索引擎搞基 自己备忘 精力总是有限的,昨天一冲动,在上海浦东外环之外订了个90米的房子,要借钱筹首付.贷款和 ...
- 使用ajax和history.pushState无刷新改变页面URL
表现 如果你使用chrome或者firefox等浏览器访问本博客.github.com.plus.google.com等网站时,细心的你会发现页面之间的点击是通过ajax异步请求的,同时页面的URL发 ...
- window.history.pushState与ajax实现无刷新更新页面url
ajax能无刷新更新数据,但是不能更新url HTML5的新API: window.history.pushState, window.history.replaceState 用户操作history ...
- Javascript history pushState onpopstate方法做AJAX SEO
参考MDN: https://developer.mozilla.org/zh-CN/docs/DOM/Manipulating_the_browser_history https://develop ...
- 使用ajax和window.history.pushState无刷新改变页面内容和地址栏URL
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
随机推荐
- Ionic2 window开发环境搭建
1.软件安装 Node.jsCordova & Ionic CLIJava SDK (一定要安装jre1.8)Android SDK 可根据链接http://www.jianshu.com/p ...
- django 实现自定义认证
1.Django自带用户认证系统 Django自带用户认证系统,这个系统支持访问控制.注册用户.关联创建者和内容等:在开发用户认证功能时的时候,可以使用Django自带用户认证系统实现: A.相关表 ...
- POJ 1163.The Triangle-动态规划
The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 50122 Accepted: 30285 De ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- boost::operators
boost 的 operators 提供了comparison operators.arithmetic operators.operators for iterators 操作.虽然使用 C++ 的 ...
- 32、Django实战第32天:我的收藏
我的收藏有3个页面:课程机构,授课教师,公开课程 课程机构 1.编辑usercenter-fav-org.html继承usercenter-base.html 2.编辑users.views.py . ...
- HTTP隧道工具HTTPTunnel
HTTP隧道工具HTTPTunnel 在很多服务器上,防火墙会限制主机的出站流量,只允许80之类的端口.如果要使用其他端口,只能通过HTTP隧道方式实现.Kali Linux提供一款HTTP隧道工 ...
- 在MYSQL中插入当前时间,就象SQLSERVER的GETDATE()一样,以及对mysql中的时间日期操作。
在看sql教程的时候,我学的是mysql,但是教程上面的一点在mysql里面是不支持的,所以就找了其他的替代的办法 sql教程上面是这样的: 通过使用类似 GETDATE() 这样的函数,DEFAUL ...
- [xsy2978]Product of Roots
$\newcommand{align}[1]{\begin{align*}#1\end{align*}}$题意:给出$f(x)=\prod\limits_{i=1}^n(a_ix+1)$和$g(x)= ...
- [BZOJ2876]骑行川藏
以前并没有发现微积分教材上有这种东西...我还是太菜了... 其实就是要在满足$\sum\limits_{i=1}^nk_is_i(v_i-v_i')^2\leq E$的同时求$\sum\limits ...