PJAX是什么鬼【转载】

背景

目前看到的不少的pc端页面点击页面某块连接的时候,本来应该是页面的某个部分更新的,可是却整个页面刷新,整个页面都闪了一下。特别是看某些图集的页面,一个页面原本就几十张图看,看完眼睛都闪瞎了。用ajax加载数据能够解决这个问题,可是也会形成另外的问题,页面没法前进和后退。支持浏览器历史的, 刷新页面的同时, 浏览器地址栏位上面的地址也是会更改, 用浏览器的回退功能也可以回退到上一个页面。要实现这样的功能, pjax就应运而生。html

原理

基于ajax+history.pushState的新技术,该技术能够无刷新改变页面的内容,而且能够改变页面的URL。HTML5的新API扩展了window.history,使历史记录点更加开放了。能够存储当前历史记录点、替换当前历史记录点、监听历史记录点。 这里说的是一个比较火的开源项目pjax,项目地址在 https://github.com/defunkt/jq... 。实际的效果见: http://pjax.heroku.com/ 没有勾选pjax的时候, 点击连接是跳转的。 勾选了以后, 连接都是变成了ajax刷新。pajx的源代码地址 https://github.com/defunkt/jq...html5

一、 咱们注意到里面的方法:jquery

$.fn.pjax = function( container, options ) {git

return this.live('click.pjax', function(event){
    handleClick(event, container, options)
  })
}

function handleClick(event, container, options) {
  $.pjax($.extend({}, defaults, options))
  ...
  event.preventDefault()
}
var pjax = $.pjax = function( options ) {
  ...
  pjax.xhr = $.ajax(options)
}

意思是 它会监听设置的连接事件,而后组装成一个带有额外的HEADER标识的ajax请求。这个请求带有X-PJAX的HEADER标识, 服务器在收到这样的请求的时候, 就知道只须要渲染部分页面返回就能够了。github

xhr.setRequestHeader('X-PJAX', 'true')
xhr.setRequestHeader('X-PJAX-Container', context.selector)

图片描述

二、pjax接受到返回的请求以后, 更新data-pjax指定的区域, 同时也会更新浏览器的地址。ajax

图片描述

options.success = function(data, status, xhr) {
  var container = extractContainer(data, xhr, options)
  ...
  if (container.title) document.title = container.title
  context.html(container.contents)
}

三、为了可以支持浏览器的后退, 利用到了history的api, 记录下来对应的信息api

pjax.state = {
  id: options.id || uniqueId(),
  url: container.url,
  container: context.selector,
  fragment: options.fragment,
  timeout: options.timeout
}

if (options.push || options.replace) {
  window.history.replaceState(pjax.state, container.title, container.url)
}

当浏览器后退的时候, 拦截事件, 根据记录的历史信息, 产生一个新的ajax请求。浏览器

$(window).bind('popstate', function(event){
  var state = event.state
  if (state && state.container) {
    var container = $(state.container)
    if (container.length) {
      ...
      var options = {
        id: state.id,
        url: state.url,
        container: container,
        push: false,
        fragment: state.fragment,
        timeout: state.timeout,
        scrollTo: false
      }

      if (contents) {
        // pjax event is deprecated
        $(document).trigger('pjax', [null, options])
        container.trigger('pjax:start', [null, options])
        // end.pjax event is deprecated
        container.trigger('start.pjax', [null, options])

        container.html(contents)
        pjax.state = state

        container.trigger('pjax:end', [null, options])
        // end.pjax event is deprecated
        container.trigger('end.pjax', [null, options])
      } else {
        $.pjax(options)
      }
      ...
    }
  }
}

为了支持fallback, 一个是在加载的时候判断浏览器是否支持history push state API:服务器

// Is pjax supported by this browser?
$.support.pjax =
  window.history && window.history.pushState && window.history.replaceState
  // pushState isn't reliable on iOS until 5.
  && !navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]|WebApps\/.+CFNetwork)/)

另外一个是当发现请求一段时间没有回复的时候(能够设置参数timeout), 直接作页面跳转。app

options.beforeSend = function(xhr, settings) {
  if (settings.timeout > 0) {
    timeoutTimer = setTimeout(function() {
      if (fire('pjax:timeout', [xhr, options]))
        xhr.abort('timeout')
    }, settings.timeout)

    // Clear timeout setting so jquerys internal timeout isn't invoked
    settings.timeout = 0

使用

引入jq和pajax的js以后初始化

(document).pjax('ul a', '#main')

官方demo

参考文章:
http://www.tuicool.com/articl...
http://www.zhangxinxu.com/wor...
https://my.oschina.net/sub/bl...

相关文章
相关标签/搜索