html5 history api介绍
history是个全局变量,即window.history
属性和方法如下:
length:历史堆栈中的记录数。
back(): 返回上一页。
foward(): 前进到下一页。
go([delta]): delta是数字,如果为0或为空则刷新本页,如果正数则前进[delta]页,如负数则后退[delta]页。
HTML5添加了以下两个方法:
pushState(data, title, [,url]):在历史堆栈顶部插入一条记录。
data为一个对象或null,会在window的popstate事件(window.onpopstate)时,作为state参数传递过去。
title为页面的标题,当前所有浏览器都忽略这个参数。
url 为页面url,不写则为当前页。
replaceState(data, title, [,url]):更改当前页面的历史记录。这种更改不会去访问该URL。
replaceState()的URL参数必须和当前页的协议(如HTTP、HTTPS)和域名完全相同(使用不同的子域都不行)
目前只有Safari 5.0+、Chrome 8.0+、Firefox 4.0+和iOS 4.2.1+支持。如果想兼容老浏览器的话,可以试试History.js,而且它还修正了一些bug。
pushState 与 replaceState 使用:
<?php $photo = isset($_GET['id'])? $_GET['id'] : 1;
?>nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta>
<title>test</title>
<script>
function go(c){
document.title = 'test' + c; //更改title
window.history.pushState({'title':'test'+c, 'photo':c}, 'test'+c, 'test.php?id='+c); // 插入前一页历史记录
window.history.replaceState({'title':'test'+c, 'photo':c},'test'+c, 'test.php?id='+c); // 更改当前历史记录
document.getElementById("photo").src = c + '.jpg';
}
window.onpopstate = function(obj){
if(obj.state!=null){
document.title = obj.state.title; // 后退时更新title
document.getElementById("photo").src = obj.state.photo + '.jpg';
}
}
</script>
<p>
<a>page 1</a>
<a>page 2</a>
<a>page 3</a>
<a>page 4</a>
</p>
<p>@@##@@.jpg" id="photo"></p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/shouce/1728" title="基于PhoneGap的HTML5的移动应用 中文WORD版"><img
src="https://img.php.cn/upload/manual/000/000/004/170658756314812.png" alt="基于PhoneGap的HTML5的移动应用 中文WORD版" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/shouce/1728" title="基于PhoneGap的HTML5的移动应用 中文WORD版" class="overflowclass">基于PhoneGap的HTML5的移动应用 中文WORD版</a>
<p class="overflowclass">本文档主要讲述的是基于PhoneGap的HTML5的移动应用;希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看</p>
</div>
<a rel="nofollow" href="/xiazai/shouce/1728" title="基于PhoneGap的HTML5的移动应用 中文WORD版" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
window.onpopstate方法:
window.onpopstate = function(event){
alert(event.state);
}
本篇文章关于HTML5 history API 的介绍,更多相关内容请关注php中文网。
相关推荐:
大量免费API接口:立即使用
涵盖生活服务API、金融科技API、企业工商API、等相关的API接口服务。免费API接口可安全、合规地连接上下游,为数据API应用能力赋能!










