{"_id":"jquery-pjax","_rev":"9-c20e65d270be99d8ddf7c9ee437d63ae","name":"jquery-pjax","time":{"modified":"2022-06-19T05:09:05.894Z","created":"2015-10-12T20:53:18.357Z","1.9.6":"2015-10-12T20:53:18.357Z","2.0.0":"2017-05-19T23:20:39.291Z","2.0.1":"2017-05-20T18:57:51.590Z"},"maintainers":[{"name":"mislav","email":"mislav.marohnic@gmail.com"}],"dist-tags":{"latest":"2.0.1"},"readme":"# pjax = pushState + ajax\n\n\n            .--.\n           /    \\\n          ## a  a\n          (   '._)\n           |'-- |\n         _.\\___/_   ___pjax___\n       .\"\\> \\Y/|<'.  '._.-'\n      /  \\ \\_\\/ /  '-' /\n      | --'\\_/|/ |   _/\n      |___.-' |  |`'`\n        |     |  |\n        |    / './\n       /__./` | |\n          \\   | |\n           \\  | |\n           ;  | |\n           /  | |\n     jgs  |___\\_.\\_\n          `-\"--'---'\n\n## Introduction\n\npjax is a jQuery plugin that uses ajax and pushState to deliver a fast browsing experience with real permalinks, page titles, and a working back button.\n\npjax works by grabbing html from your server via ajax and replacing the content\nof a container on your page with the ajax'd html. It then updates the browser's\ncurrent URL using pushState without reloading your page's layout or any\nresources (JS, CSS), giving the appearance of a fast, full page load. But really\nit's just ajax and pushState.\n\nFor [browsers that don't support pushState][compat] pjax fully degrades.\n\n## Overview\n\npjax is not fully automatic. You'll need to setup and designate a containing element on your page that will be replaced when you navigate your site.\n\nConsider the following page.\n\n``` html\n<!DOCTYPE html>\n<html>\n<head>\n  <!-- styles, scripts, etc -->\n</head>\n<body>\n  <h1>My Site</h1>\n  <div class=\"container\" id=\"pjax-container\">\n    Go to <a href=\"/page/2\">next page</a>.\n  </div>\n</body>\n</html>\n```\n\nWe want pjax to grab the URL `/page/2` then replace `#pjax-container` with\nwhatever it gets back. No styles or scripts will be reloaded and even the `<h1>`\ncan stay the same - we just want to change the `#pjax-container` element.\n\nWe do this by telling pjax to listen on `a` tags and use `#pjax-container` as the target container:\n\n``` javascript\n$(document).pjax('a', '#pjax-container')\n```\n\nNow when someone in a pjax-compatible browser clicks \"next page\" the content of `#pjax-container` will be replaced with the body of `/page/2`.\n\nMagic! Almost. You still need to configure your server to look for pjax requests and send back pjax-specific content.\n\nThe pjax ajax request sends an `X-PJAX` header so in this example (and in most cases) we want to return just the content of the page without any layout for any requests with that header.\n\nHere's what it might look like in Rails:\n\n``` ruby\ndef index\n  if request.headers['X-PJAX']\n    render :layout => false\n  end\nend\n```\n\nIf you'd like a more automatic solution than pjax for Rails check out [Turbolinks][].\n\nAlso check out [RailsCasts #294: Playing with PJAX][railscasts].\n\n## Installation\n\n### bower\n\nVia [Bower][]:\n\n```\n$ bower install jquery-pjax\n```\n\nOr, add `jquery-pjax` to your app's `bower.json`.\n\n``` json\n  \"dependencies\": {\n    \"jquery-pjax\": \"latest\"\n  }\n```\n\n### standalone\n\npjax can be downloaded directly into your app's public directory - just be sure you've loaded jQuery first.\n\n```\ncurl -LO https://raw.github.com/defunkt/jquery-pjax/master/jquery.pjax.js\n```\n\n**WARNING** Do not hotlink the raw script url. GitHub is not a CDN.\n\n## Dependencies\n\nRequires jQuery 1.8.x or higher.\n\n## Compatibility\n\npjax only works with [browsers that support the `history.pushState`\nAPI][compat]. When the API isn't supported pjax goes into fallback mode:\n`$.fn.pjax` calls will be a no-op and `$.pjax` will hard load the given URL.\n\nFor debugging purposes, you can intentionally disable pjax even if the browser supports `pushState`. Just call `$.pjax.disable()`. To see if pjax is actually supports `pushState`, check `$.support.pjax`.\n\n## Usage\n\n### `$.fn.pjax`\n\nLet's talk more about the most basic way to get started:\n\n``` javascript\n$(document).pjax('a', '#pjax-container')\n```\n\nThis will enable pjax on all links and designate the container as `#pjax-container`.\n\nIf you are migrating an existing site you probably don't want to enable pjax everywhere just yet. Instead of using a global selector like `a` try annotating pjaxable links with `data-pjax`, then use `'a[data-pjax]'` as your selector.\n\nOr try this selector that matches any `<a data-pjax href=>` links inside a `<div data-pjax>` container.\n\n``` javascript\n$(document).pjax('[data-pjax] a, a[data-pjax]', '#pjax-container')\n```\n\n#### Arguments\n\nThe synopsis for the `$.fn.pjax` function is:\n\n``` javascript\n$(document).pjax(selector, [container], options)\n```\n\n1. `selector` is a string to be used for click [event delegation][$.fn.on].\n2. `container` is a string selector that uniquely identifies the pjax container.\n3. `options` is an object with keys described below.\n\n##### pjax options\n\nkey | default | description\n----|---------|------------\n`timeout` | 650 | ajax timeout in milliseconds after which a full refresh is forced\n`push` | true | use [pushState][] to add a browser history entry upon navigation\n`replace` | false | replace URL without adding browser history entry\n`maxCacheLength` | 20 | maximum cache size for previous container contents\n`version` | | a string or function returning the current pjax version\n`scrollTo` | 0 | vertical position to scroll to after navigation. To avoid changing scroll position, pass `false`.\n`type` | `\"GET\"` | see [$.ajax][]\n`dataType` | `\"html\"` | see [$.ajax][]\n`container` | | CSS selector for the element where content should be replaced\n`url` | link.href | a string or function that returns the URL for the ajax request\n`target` | link | eventually the `relatedTarget` value for [pjax events](#events)\n`fragment` | | CSS selector for the fragment to extract from ajax response\n\nYou can change the defaults globally by writing to the `$.pjax.defaults` object:\n\n``` javascript\n$.pjax.defaults.timeout = 1200\n```\n\n### `$.pjax.click`\n\nThis is a lower level function used by `$.fn.pjax` itself. It allows you to get a little more control over the pjax event handling.\n\nThis example uses the current click context to set an ancestor as the container:\n\n``` javascript\nif ($.support.pjax) {\n  $(document).on('click', 'a[data-pjax]', function(event) {\n    var container = $(this).closest('[data-pjax-container]')\n    $.pjax.click(event, {container: container})\n  })\n}\n```\n\n**NOTE** Use the explicit `$.support.pjax` guard. We aren't using `$.fn.pjax` so we should avoid binding this event handler unless the browser is actually going to use pjax.\n\n### `$.pjax.submit`\n\nSubmits a form via pjax.\n\n``` javascript\n$(document).on('submit', 'form[data-pjax]', function(event) {\n  $.pjax.submit(event, '#pjax-container')\n})\n```\n\n### `$.pjax.reload`\n\nInitiates a request for the current URL to the server using pjax mechanism and replaces the container with the response. Does not add a browser history entry.\n\n``` javascript\n$.pjax.reload('#pjax-container', options)\n```\n\n### `$.pjax`\n\nManual pjax invocation. Used mainly when you want to start a pjax request in a handler that didn't originate from a click. If you can get access to a click `event`, consider `$.pjax.click(event)` instead.\n\n``` javascript\nfunction applyFilters() {\n  var url = urlForFilters()\n  $.pjax({url: url, container: '#pjax-container'})\n}\n```\n\n### Events\n\nAll pjax events except `pjax:click` & `pjax:clicked` are fired from the pjax\ncontainer, not the link that was clicked.\n\n<table>\n<tr>\n  <th>event</th>\n  <th>cancel</th>\n  <th>arguments</th>\n  <th>notes</th>\n</tr>\n<tr>\n  <th colspan=4>event lifecycle upon following a pjaxed link</th>\n</tr>\n<tr>\n  <td><code>pjax:click</code></td>\n  <td>✔︎</td>\n  <td><code>options</code></td>\n  <td>fires from a link that got activated; cancel to prevent pjax</td>\n</tr>\n<tr>\n  <td><code>pjax:beforeSend</code></td>\n  <td>✔︎</td>\n  <td><code>xhr, options</code></td>\n  <td>can set XHR headers</td>\n</tr>\n<tr>\n  <td><code>pjax:start</code></td>\n  <td></td>\n  <td><code>xhr, options</code></td>\n  <td></td>\n</tr>\n<tr>\n  <td><code>pjax:send</code></td>\n  <td></td>\n  <td><code>xhr, options</code></td>\n  <td></td>\n</tr>\n<tr>\n  <td><code>pjax:clicked</code></td>\n  <td></td>\n  <td><code>options</code></td>\n  <td>fires after pjax has started from a link that got clicked</td>\n</tr>\n<tr>\n  <td><code>pjax:beforeReplace</code></td>\n  <td></td>\n  <td><code>contents, options</code></td>\n  <td>before replacing HTML with content loaded from the server</td>\n</tr>\n<tr>\n  <td><code>pjax:success</code></td>\n  <td></td>\n  <td><code>data, status, xhr, options</code></td>\n  <td>after replacing HTML content loaded from the server</td>\n</tr>\n<tr>\n  <td><code>pjax:timeout</code></td>\n  <td>✔︎</td>\n  <td><code>xhr, options</code></td>\n  <td>fires after <code>options.timeout</code>; will hard refresh unless canceled</td>\n</tr>\n<tr>\n  <td><code>pjax:error</code></td>\n  <td>✔︎</td>\n  <td><code>xhr, textStatus, error, options</code></td>\n  <td>on ajax error; will hard refresh unless canceled</td>\n</tr>\n<tr>\n  <td><code>pjax:complete</code></td>\n  <td></td>\n  <td><code>xhr, textStatus, options</code></td>\n  <td>always fires after ajax, regardless of result</td>\n</tr>\n<tr>\n  <td><code>pjax:end</code></td>\n  <td></td>\n  <td><code>xhr, options</code></td>\n  <td></td>\n</tr>\n<tr>\n  <th colspan=4>event lifecycle on browser Back/Forward navigation</th>\n</tr>\n<tr>\n  <td><code>pjax:popstate</code></td>\n  <td></td>\n  <td></td>\n  <td>event <code>direction</code> property: &quot;back&quot;/&quot;forward&quot;</td>\n</tr>\n<tr>\n  <td><code>pjax:start</code></td>\n  <td></td>\n  <td><code>null, options</code></td>\n  <td>before replacing content</td>\n</tr>\n<tr>\n  <td><code>pjax:beforeReplace</code></td>\n  <td></td>\n  <td><code>contents, options</code></td>\n  <td>right before replacing HTML with content from cache</td>\n</tr>\n<tr>\n  <td><code>pjax:end</code></td>\n  <td></td>\n  <td><code>null, options</code></td>\n  <td>after replacing content</td>\n</tr>\n</table>\n\n`pjax:send` & `pjax:complete` are a good pair of events to use if you are implementing a\nloading indicator. They'll only be triggered if an actual XHR request is made,\nnot if the content is loaded from cache:\n\n``` javascript\n$(document).on('pjax:send', function() {\n  $('#loading').show()\n})\n$(document).on('pjax:complete', function() {\n  $('#loading').hide()\n})\n```\n\nAn example of canceling a `pjax:timeout` event would be to disable the fallback\ntimeout behavior if a spinner is being shown:\n\n``` javascript\n$(document).on('pjax:timeout', function(event) {\n  // Prevent default timeout redirection behavior\n  event.preventDefault()\n})\n```\n\n### Server side\n\nServer configuration will vary between languages and frameworks. The following example shows how you might configure Rails.\n\n``` ruby\ndef index\n  if request.headers['X-PJAX']\n    render :layout => false\n  end\nend\n```\n\nAn `X-PJAX` request header is set to differentiate a pjax request from normal XHR requests. In this case, if the request is pjax, we skip the layout html and just render the inner contents of the container.\n\n[Check if there is a pjax plugin][plugins] for your favorite server framework.\n\n#### Response types that force a reload\n\nBy default, pjax will force a full reload of the page if it receives one of the\nfollowing responses from the server:\n\n* Page content that includes `<html>` when `fragment` selector wasn't explicitly\n  configured. Pjax presumes that the server's response hasn't been properly\n  configured for pjax. If `fragment` pjax option is given, pjax will simply\n  extract the content to insert into the DOM based on that selector.\n\n* Page content that is blank. Pjax assumes that the server is unable to deliver\n  proper pjax contents.\n\n* HTTP response code that is 4xx or 5xx, indicating some server error.\n\n#### Affecting the browser URL\n\nIf the server needs to affect the URL which will appear in the browser URL after\npjax navigation (like HTTP redirects work for normal requests), it can set the\n`X-PJAX-URL` header:\n\n``` ruby\ndef index\n  request.headers['X-PJAX-URL'] = \"http://example.com/hello\"\nend\n```\n\n#### Layout Reloading\n\nLayouts can be forced to do a hard reload when assets or html changes.\n\nFirst set the initial layout version in your header with a custom meta tag.\n\n``` html\n<meta http-equiv=\"x-pjax-version\" content=\"v123\">\n```\n\nThen from the server side, set the `X-PJAX-Version` header to the same.\n\n``` ruby\nif request.headers['X-PJAX']\n  response.headers['X-PJAX-Version'] = \"v123\"\nend\n```\n\nDeploying a deploy, bumping the version constant to force clients to do a full reload the next request getting the new layout and assets.\n\n[compat]: http://caniuse.com/#search=pushstate\n[$.fn.on]: http://api.jquery.com/on/\n[$.ajax]: http://api.jquery.com/jQuery.ajax/\n[pushState]: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history#Adding_and_modifying_history_entries\n[plugins]: https://gist.github.com/4283721\n[turbolinks]: https://github.com/rails/turbolinks\n[railscasts]: http://railscasts.com/episodes/294-playing-with-pjax\n[bower]: https://github.com/twitter/bower\n","versions":{"2.0.0":{"name":"jquery-pjax","version":"2.0.0","main":"jquery.pjax.js","repository":{"type":"git","url":"git+https://github.com/defunkt/jquery-pjax.git"},"licenses":[{"type":"MIT","url":"https://github.com/defunkt/jquery-pjax/blob/master/LICENSE"}],"files":["LICENSE","jquery.pjax.js"],"gitHead":"573f74c42e01eed2bcf3cb998bd3b476a7645fe2","description":"","bugs":{"url":"https://github.com/defunkt/jquery-pjax/issues"},"homepage":"https://github.com/defunkt/jquery-pjax#readme","_id":"jquery-pjax@2.0.0","scripts":{},"_shasum":"840578c049a998b3116388f385da229008c8c6ae","_from":".","_npmVersion":"4.2.0","_nodeVersion":"7.9.0","_npmUser":{"name":"mislav","email":"mislav.marohnic@gmail.com"},"dist":{"shasum":"840578c049a998b3116388f385da229008c8c6ae","tarball":"https://registry.npmjs.org/jquery-pjax/-/jquery-pjax-2.0.0.tgz","integrity":"sha512-/TC4giUencrv4WiP44sdsuldnBzRdZzVZb0bxx8rD65L+LcHiBZ3NDLfgoRP5Uj3E08Eqam2B8Fh5kYGqg3gTQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEW+eOXYE4IpcL7wwdiPVvEawX859UD3OEEDIiLydwSLAiBlzfmgJ2uf3LEssfQQsj3sSXAyiVtkBr6fqoJBt3Q7vw=="}]},"maintainers":[{"name":"mislav","email":"mislav.marohnic@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jquery-pjax-2.0.0.tgz_1495236038240_0.08272206294350326"}},"2.0.1":{"name":"jquery-pjax","description":"jQuery plugin for ajax + pushState navigation","version":"2.0.1","main":"jquery.pjax.js","repository":{"type":"git","url":"git+https://github.com/defunkt/jquery-pjax.git"},"license":"MIT","files":["LICENSE","jquery.pjax.js"],"devDependencies":{"eslint":"^3.19.0"},"scripts":{"test":"./script/test"},"gitHead":"a24470733f3af7f3b0b63be6a5f3eb2552925629","bugs":{"url":"https://github.com/defunkt/jquery-pjax/issues"},"homepage":"https://github.com/defunkt/jquery-pjax#readme","_id":"jquery-pjax@2.0.1","_shasum":"6b3a1ba16e644e624bdcfe72eb6b3d96a846f5f2","_from":".","_npmVersion":"4.2.0","_nodeVersion":"7.9.0","_npmUser":{"name":"mislav","email":"mislav.marohnic@gmail.com"},"dist":{"shasum":"6b3a1ba16e644e624bdcfe72eb6b3d96a846f5f2","tarball":"https://registry.npmjs.org/jquery-pjax/-/jquery-pjax-2.0.1.tgz","integrity":"sha512-xMSUMh5p0f0O2XMh13YbkS94NWuWkV9k+3ouECvVWb9Li6OCF7BVinotGwbUYDKYWA0PCoTq64dTYiJwmN6H6Q==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDIZemnKErWZmVYjPim/c6f/IwKeU3dhmXA1+dTnVwrrAIhAK0ffqcUxrfakJ6bh07zvXf7ODNSQxdx1GO/mL4cl/yH"}]},"maintainers":[{"name":"mislav","email":"mislav.marohnic@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/jquery-pjax-2.0.1.tgz_1495306670582_0.37432699953205884"}}},"homepage":"https://github.com/defunkt/jquery-pjax#readme","repository":{"type":"git","url":"git+https://github.com/defunkt/jquery-pjax.git"},"bugs":{"url":"https://github.com/defunkt/jquery-pjax/issues"},"readmeFilename":"README.md","description":"jQuery plugin for ajax + pushState navigation","license":"MIT"}