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...

相關文章
相關標籤/搜索