{"id":13395015,"url":"https://github.com/defunkt/jquery-pjax","last_synced_at":"2025-05-14T08:05:19.013Z","repository":{"id":221130760,"uuid":"1413492","full_name":"defunkt/jquery-pjax","owner":"defunkt","description":"pushState + ajax = pjax","archived":false,"fork":false,"pushed_at":"2022-11-30T22:59:46.000Z","size":872,"stargazers_count":16706,"open_issues_count":93,"forks_count":1958,"subscribers_count":550,"default_branch":"master","last_synced_at":"2025-05-07T07:18:56.473Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pjax.herokuapp.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/defunkt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-02-26T02:44:18.000Z","updated_at":"2025-05-06T06:12:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"d307df1d-8ebe-4a51-8ca5-172d346e15a4","html_url":"https://github.com/defunkt/jquery-pjax","commit_stats":{"total_commits":410,"total_committers":72,"mean_commits":5.694444444444445,"dds":0.6146341463414634,"last_synced_commit":"153262eda33e31119eabb97cd5f14365580a3b35"},"previous_names":["defunkt/jquery-pjax"],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defunkt%2Fjquery-pjax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defunkt%2Fjquery-pjax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defunkt%2Fjquery-pjax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defunkt%2Fjquery-pjax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/defunkt","download_url":"https://codeload.github.com/defunkt/jquery-pjax/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253850874,"owners_count":21973672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:01:39.222Z","updated_at":"2025-05-14T08:05:18.996Z","avatar_url":"https://github.com/defunkt.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","HTTP 平台资源库","Javascript","\u003ca name=\"JavaScript\"\u003e\u003c/a\u003eJavaScript","8. 路由和链接(Routing And URLs)","Programming Languages","Examples"],"sub_categories":["Browser","Misc","JavaScript","Page Navigation"],"readme":"# pjax = pushState + ajax\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 fetching HTML from your server via ajax and replacing the content\nof a container element on your page with the loaded HTML. It then updates the\ncurrent URL in the browser using pushState. This results in faster page\nnavigation for two reasons:\n\n* No page resources (JS, CSS) get re-executed or re-applied;\n* If the server is configured for pjax, it can render only partial page\n  contents and thus avoid the potentially costly full layout render.\n\n### Status of this project\n\njquery-pjax is **largely unmaintained** at this point. It might continue to\nreceive important bug fixes, but _its feature set is frozen_ and it's unlikely\nthat it will get new features or enhancements.\n\n## Installation\n\npjax depends on jQuery 1.8 or higher.\n\n### npm\n\n```\n$ npm install jquery-pjax\n```\n\n### standalone script\n\nDownload and include `jquery.pjax.js` in your web page:\n\n```\ncurl -LO https://raw.github.com/defunkt/jquery-pjax/master/jquery.pjax.js\n```\n\n## Usage\n\n### `$.fn.pjax`\n\nThe simplest and most common use of pjax looks like this:\n\n``` javascript\n$(document).pjax('a', '#pjax-container')\n```\n\nThis will enable pjax on all links on the page and designate the container as `#pjax-container`.\n\nIf you are migrating an existing site, you probably don't want to enable pjax\neverywhere just yet. Instead of using a global selector like `a`, try annotating\npjaxable links with `data-pjax`, then use `'a[data-pjax]'` as your selector. Or,\ntry this selector that matches any `\u003ca data-pjax href=\u003e` links inside a `\u003cdiv\ndata-pjax\u003e` container:\n\n``` javascript\n$(document).pjax('[data-pjax] a, a[data-pjax]', '#pjax-container')\n```\n\n#### Server-side configuration\n\nIdeally, your server should detect pjax requests by looking at the special\n`X-PJAX` HTTP header, and render only the HTML meant to replace the contents of\nthe container element (`#pjax-container` in our example) without the rest of\nthe page layout. Here is an example of how this might be done in Ruby on Rails:\n\n``` ruby\ndef index\n  if request.headers['X-PJAX']\n    render :layout =\u003e false\n  end\nend\n```\n\nIf you'd like a more automatic solution than pjax for Rails check out [Turbolinks][].\n\n[Check if there is a pjax plugin][plugins] for your favorite server framework.\n\nAlso check out [RailsCasts #294: Playing with PJAX][railscasts].\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 element 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    var containerSelector = '#' + container.id\n    $.pjax.click(event, {container: containerSelector})\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` \u0026 `pjax:clicked` are fired from the pjax\ncontainer element.\n\n\u003ctable\u003e\n\u003ctr\u003e\n  \u003cth\u003eevent\u003c/th\u003e\n  \u003cth\u003ecancel\u003c/th\u003e\n  \u003cth\u003earguments\u003c/th\u003e\n  \u003cth\u003enotes\u003c/th\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003cth colspan=4\u003eevent lifecycle upon following a pjaxed link\u003c/th\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:click\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e✔︎\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003eoptions\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003efires from a link that got activated; cancel to prevent pjax\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:beforeSend\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e✔︎\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003exhr, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003ecan set XHR headers\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:start\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003exhr, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:send\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003exhr, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:clicked\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003eoptions\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003efires after pjax has started from a link that got clicked\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:beforeReplace\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003econtents, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003ebefore replacing HTML with content loaded from the server\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:success\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003edata, status, xhr, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003eafter replacing HTML content loaded from the server\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:timeout\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e✔︎\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003exhr, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003efires after \u003ccode\u003eoptions.timeout\u003c/code\u003e; will hard refresh unless canceled\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:error\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e✔︎\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003exhr, textStatus, error, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003eon ajax error; will hard refresh unless canceled\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:complete\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003exhr, textStatus, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003ealways fires after ajax, regardless of result\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:end\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003exhr, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003cth colspan=4\u003eevent lifecycle on browser Back/Forward navigation\u003c/th\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:popstate\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003eevent \u003ccode\u003edirection\u003c/code\u003e property: \u0026quot;back\u0026quot;/\u0026quot;forward\u0026quot;\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:start\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003enull, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003ebefore replacing content\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:beforeReplace\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003econtents, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003eright before replacing HTML with content from cache\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n  \u003ctd\u003e\u003ccode\u003epjax:end\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003c/td\u003e\n  \u003ctd\u003e\u003ccode\u003enull, options\u003c/code\u003e\u003c/td\u003e\n  \u003ctd\u003eafter replacing content\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\n`pjax:send` \u0026 `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## Advanced configuration\n\n### Reinitializing plugins/widget on new page content\n\nThe whole point of pjax is that it fetches and inserts new content _without_\nrefreshing the page. However, other jQuery plugins or libraries that are set to\nreact on page loaded event (such as `DOMContentLoaded`) will not pick up on\nthese changes. Therefore, it's usually a good idea to configure these plugins to\nreinitialize in the scope of the updated page content. This can be done like so:\n\n``` js\n$(document).on('ready pjax:end', function(event) {\n  $(event.target).initializeMyPlugin()\n})\n```\n\nThis will make `$.fn.initializeMyPlugin()` be called at the document level on\nnormal page load, and on the container level after any pjax navigation (either\nafter clicking on a link or going Back in the browser).\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 `\u003chtml\u003e` 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 extract the\n  content 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\u003cmeta http-equiv=\"x-pjax-version\" content=\"v123\"\u003e\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\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefunkt%2Fjquery-pjax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdefunkt%2Fjquery-pjax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefunkt%2Fjquery-pjax/lists"}