使用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 ...
随机推荐
- ChannelFactory
通过前几篇的学习,我们简单了解了WCF的服务端-客户端模型,可以建立一个简单的WCF通信程序,并且可以把我们的服务寄宿在IIS中了.我们不禁感叹WCF模型的简单,寥寥数行代码和配置,就可以把通信建立起 ...
- codeforces Round 442 B Nikita and string【前缀和+暴力枚举分界点/线性DP】
B. Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Python的异步编程[0] -> 协程[1] -> 使用协程建立自己的异步非阻塞模型
使用协程建立自己的异步非阻塞模型 接下来例子中,将使用纯粹的Python编码搭建一个异步模型,相当于自己构建的一个asyncio模块,这也许能对asyncio模块底层实现的理解有更大的帮助.主要参考为 ...
- ( 转 ) 数据库BTree索引、Hash索引、Bitmap位图索引的优缺点
测试于:MySQL 5.5.25 当前测试的版本是Mysql 5.5.25只有BTree和Hash两种索引类型,默认为BTree.Oracle或其他类型数据库中会有Bitmap索引(位图索引),这里作 ...
- 在小程序开发中使用 npm
微信小程序在发布之初没有对 npm 的支持功能,这也是目前很多前端开发人员在熟悉了 npm 生态环境后,对微信小程序诟病的地方. 微信小程序在 2.2.1 版本后增加了对 npm 包加载的支持,使得小 ...
- Eclipse Build all and build project not working - jar missing
Eclipse Build all and build project not working - jar missing
- [ZOJ3316]Game
题意:有一个棋盘,棋盘上有一些棋子,两个人轮流拿棋,第一个人可以随意拿,以后每一个人拿走的棋子与上一个人拿走的棋子的曼哈顿距离不得超过$L$,无法拿棋的人输,问后手能否胜利 把棋子看做点,如果两个棋子 ...
- 【记忆化搜索】bzoj1079 [SCOI2008]着色方案
#include<cstring> #include<cstdio> using namespace std; #define MOD 1000000007 typedef l ...
- IDEA ULTIMATE 2019.1 注册码,亲测可用
在 hosts 文件里加入如下的配置: 0.0.0.0 account.jetbrains.com 0.0.0.0 www.jetbrains.com # 2019.1得加这个 注册码: N757JE ...
- 【MySQL笔记】Excel数据导入Mysql数据库的实现方法——Navicat
很多公司尤其有点年头的公司,财务业务部门的各种表单都是excel来做的表格,随着互联网的发展各种业务流程都电子化流程化了,再在茫茫多的文档中去查找某一个年份月份的报告是件相当枯燥的事,所以都在想办法将 ...