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

it2022-05-05  100

表现

如果你使用chrome或者firefox等浏览器访问本博客、github.com、plus.google.com等网站时,细心的你会发现页面之间的点击是通过ajax异步请求的,同时页面的URL发生了了改变。并且能够很好的支持浏览器前进和后退。

是什么有这么强大的功能呢?

HTML5里引用了新的API,history.pushState和history.replaceState,就是通过这个接口做到无刷新改变页面URL的。

与传统的AJAX的区别

传统的ajax有如下的问题:

1、可以无刷新改变页面内容,但无法改变页面URL

2、为了更好的可访问性,内容发生改变后,通常改变URL的hash

3、hash的方式不能很好的处理浏览器的前进、后退等问题

4、进而浏览器引入了onhashchange的接口,不支持的浏览器只能定时去判断hash是否改变

5、但这种方式对搜索引擎很不友好

6、twitter和google约定了使用#!xxx(即hash第一个字符为!),搜索引擎进行支持。

为了解决传统ajax带来的问题,HTML5里引入了新的API,即:history.pushState, history.replaceState

可以通过pushState和replaceState接口操作浏览器历史,并且改变当前页面的URL。

pushState是将指定的URL添加到浏览器历史里,replaceState是将指定的URL替换当前的URL。

如何使用

var state = { title: title, url: options.url, otherkey: othervalue }; window.history.pushState(state, document.title, url);

state对象除了要title和url之外,你也可以添加其他的数据,比如:还想将一些发送ajax的配置给保存起来。

replaceState和pushState是相似的,这里就不多介绍了。

如何响应浏览器的前进、后退操作

window对象上提供了onpopstate事件,上面传递的state对象会成为event的子对象,这样就可以拿到存储的title和URL了。

window.addEventListener('popstate', function(e){ if (history.state){ var state = e.state; //do something(state.url, state.title); } }, false);

这样就可以结合ajax和pushState完美的进行无刷新浏览了。

一些限制

1、传递的URL必须是同域下的,无法跨域

2、state对象虽然可以存储很多自定义的属性,但对于不可序列化的对象则不能存储,如:DOM对象。

对应后端的一些处理

这种模式下除了当前使用ajax可以无刷新浏览外,还要保证直接请求改变的URL后也可以正常浏览,所以后端要对这些处理下。

1、对使用pushState的ajax发送一个特殊的头,如: setRequestHeader('PJAX', 'true')。

2、后端获取到有PJAX=true的header时,将页面中通用的部分都不输出。比如:PHP可以通过下面的判断

function is_pjax(){ return array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX'] === 'true'; }

虽然接口上只有pushState、replaceState、onpopstate,但在使用的时候需要做很多的处理。

针对这个已经写好了一个基于jquery的插件,接下来的文章会详细介绍如何使用pjax(ajax+pushState)进行无刷新改变URL浏览。

@update - 2012.03.06

已经将ajax+history.pushState封装成pjax, 项目地址: https://github.com/welefen/pjax, 目前支持jquery, qwrap, kissy 3个版本

参考资料:

1、Introducing the HTML5 History API

2、Manipulating the browser history

3、Session history and navigation

本文链接:http://www.welefen.com/use-ajax-and-pushstate.html

 

下面做个简单的测试

index.html页面

<!DOCTYPE html> <!-- 这是index.html --> <html lang="zh-cn"> <head> <meta charset="utf-8"> <title>演示</title> <style> .digits{ display:inline-block; height:30px; margin-right:50px; } .digits i{ background:url(number.png) no-repeat; display:inline-block; float:left; height:30px; width:18px; } .digits b{ /*background:url(number.png) no-repeat 0 -398px;*/ display:inline-block; float:left; height:30px; width:10px; } #container{ position: absolute; bottom: 0; right: 0; } </style> <script type="text/javascript" src="http://cdn.guojj.com/jquery/jquery-1.8.3.min.js?v2015"></script> <script type="text/javascript" src="js/jquert.pjax.js" charset="utf-8"></script> </head> <body> <a href="index2.html" >2</a><!-- 这是同目录下的index2.html --> <div id="a" class="digits"></div> <div id="b" class="digits"></div> <div id="container">1</div> <script> $(function(){ $('#container').bind('pjax.start', function(){ alert('start') }) $('#container').bind('pjax.end', function(){ alert('end') }) $.pjax({ selector: 'a', container: '#container', //内容替换的容器 show: 'fade', //展现的动画,支持默认和fade, 可以自定义动画方式,这里为自定义的function即可。 cache: false, //是否使用缓存 storage: false, //是否使用本地存储 titleSuffix: 'abd', //标题后缀 filter: function(){}, callback: function(){} }) }) </script> </body> </html>

同一目录下的index2.html页面,要插入的内容,那么不能有公共的html头部的,如果要引入页面需要后台进行过滤

<head> <meta charset="utf-8"> <title>演示</title> <style> .digits{ display:inline-block; height:30px; margin-right:50px; } .digits i{ background:url(number.png) no-repeat; display:inline-block; float:left; height:30px; width:18px; } .digits b{ /*background:url(number.png) no-repeat 0 -398px;*/ display:inline-block; float:left; height:30px; width:10px; } </style> </head> <body> <img src="number.png"> </body>

 

转载于:https://www.cnblogs.com/lichuntian/p/5101625.html

相关资源:各显卡算力对照表!

最新回复(0)