6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
18
<a href="#toc" class="jump">Jump to Table of Contents</a>
22
<div class="yui3-u-3-4">
24
<div class="content"><div class="intro">
26
<a href="http://pjax.heroku.com/">Pjax</a> is a technique that allows you to progressively enhance normal links on a page so that clicks result in the linked content being loaded via Ajax and the URL being updated using HTML5 <code>pushState</code>, avoiding a full page load. In browsers that don't support <code>pushState</code> or that have JavaScript disabled, link clicks will result in a normal full page load. The Pjax Utility makes it easy to add this functionality to existing pages.
30
<h2 id="getting-started">Getting Started</h2>
33
To include the source files for Pjax and its dependencies, first load
34
the YUI seed file if you haven't already loaded it.
37
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js"></script></pre>
41
Next, create a new YUI instance for your application and populate it with the
42
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
43
YUI will automatically load any dependencies required by the modules you
47
<pre class="code prettyprint"><script>
48
// Create a new YUI instance and populate it with the required modules.
49
YUI().use('pjax', function (Y) {
50
// Pjax is available and ready for use. Add implementation
51
// code here.
53
</script></pre>
57
For more information on creating YUI instances and on the
58
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
59
documentation for the <a href="../yui/index.html">YUI Global Object</a>.
63
<h2 id="using-pjax">Using Pjax</h2>
65
<h3 id="quick-start">Quick Start</h3>
68
It's easy to add pjax functionality to any page with just a few lines of code. You don't even need any special server-side logic. Here's a simple example:
71
<pre class="code prettyprint"><h1>Links</h1>
74
<li><a href="page1.html" class="yui3-pjax">Page One</a></li>
75
<li><a href="page2.html" class="yui3-pjax">Page Two</a></li>
76
<li><a href="page3.html" class="yui3-pjax">Page Three</a></li>
79
<div id="content"></div>
81
<script src="http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js"></script>
83
YUI().use('pjax', function (Y) {
84
new Y.Pjax({container: '#content'});
86
</script></pre>
90
This creates a new Pjax instance that, by default, listens for clicks on any
91
link on the page that has a <code>yui3-pjax</code> class. When a <code>yui3-pjax</code> link is clicked, its URL will be loaded via Ajax and the loaded content will be inserted into the <code>#content</code> div, replacing its existing contents. If the loaded page includes an HTML <code><title></code> element, the current page's title will also be replaced with the new title.
95
One important thing to note is that since Pjax uses <a href="https://developer.mozilla.org/en/XMLHttpRequest"><code>XMLHttpRequest</code></a> under the hood, it can only load URLs from the <a href="https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript">same origin</a> as the current page. This means the link URL must share the same protocol, port, and host as the current page. Pjax will ignore URLs it can't load, resulting in a full page load for those URLs.
98
<h3 id="instantiating-pjax">Instantiating Pjax</h3>
101
There are two ways to instantiate the Pjax Utility: you can load the <code>pjax-plugin</code> module and use the <code>Y.Plugin.Pjax</code> Node plugin, or you can load the <code>pjax</code> module and create a standalone instance of the <code>Y.Pjax</code> class.
105
Both instantiation methods provide the same core functionality; they only differ in how they're instantiated. Feel free to use whichever one you prefer.
108
<h4 id="as-a-plugin">As a Plugin</h4>
111
To instantiate Pjax as a plugin, load the <code>pjax-plugin</code> module and then plug the <code>Y.Plugin.Pjax</code> class into a Node instance. The node will be used as the container element, and content loaded via Pjax will replace the contents of the node.
114
<pre class="code prettyprint">YUI().use('pjax-plugin', function (Y) {
115
Y.one('#content').plug(Y.Plugin.Pjax);
120
You may optionally pass configuration attributes as the second argument to <code>plug()</code>:
123
<pre class="code prettyprint">Y.one('#content').plug(Y.Plugin.Pjax, {
124
linkSelector: 'a.pjax',
129
<h4 id="as-a-class">As a Class</h4>
132
To instantiate Pjax as a class, load the <code>pjax</code> module and then create a new instance of <code>Y.Pjax</code>, specifying the node you want to use as a content container. You may optionally provide additional configuration attributes as well.
135
<pre class="code prettyprint">YUI().use('pjax', function (Y) {
136
new Y.Pjax({container: '#content'});
140
<h3 id="configuring-pjax">Configuring Pjax</h3>
143
All configuration attributes are optional, although you'll usually want to at least specify a <code>container</code> node if you aren't using the Pjax plugin.
146
<h4 id="config-attributes">Config Attributes</h4>
160
<td><code>addPjaxParam</code></td>
162
<td><code>true</code></td>
165
If <code>true</code>, a "pjax=1" query parameter will be appended to all URLs requested via Pjax.
169
Browsers ignore HTTP request headers when caching content, so if the same URL is used to request a partial Pjax page and a full page, the browser will cache them under the same key and may later load the cached partial page when the user actually requests a full page (or vice versa).
173
To prevent this, we can add a bogus query parameter to the URL so that Pjax URLs will always be cached separately from non-Pjax URLs.
179
<td><code>container</code></td>
181
<td><code>null</code></td>
184
Node into which content should be inserted when a page is loaded via Pjax. This node's existing contents will be removed to make way for the new content.
188
If not set, loaded content will not be automatically inserted into the page.
194
<td><code>contentSelector</code></td>
196
<td><code>null</code></td>
199
CSS selector used to extract a specific portion of the content of a page loaded via Pjax.
203
For example, if you wanted to load the page <code>example.html</code> but only use the content within an element with the id "pjax-content", you'd set
204
<code>contentSelector</code> to "#pjax-content".
208
If not set, the entire page will be used.
214
<td><code>linkSelector</code></td>
216
<td style="white-space:nowrap;"><code>"a.yui3-pjax"</code></td>
219
CSS selector string used to filter link click events so that only the links which match it will have the enhanced navigation behavior of Pjax applied.
223
When a link is clicked and that link matches this selector, Pjax will attempt to load it via Ajax. If HTML5 history is not supported, or if the link was middle-clicked, right-clicked, or clicked while a modifier key was pressed, the link click will be handled by the browser just like any old link.
229
<td><code>navigateOnHash</code></td>
231
<td><code>false</code></td>
234
Whether navigating to a hash-fragment identifier on the current page should be enhanced and cause the <code>navigate</code> event to fire.
238
By default Pjax allows the browser to perform its default action when a user is navigating within a page by clicking in-page links (e.g. <code><a href="#top">Top of page</a></code>) and does not attempt to interfere or enhance in-page navigation.
244
<td><code>scrollToTop</code></td>
246
<td><code>true</code></td>
249
Whether the page should be scrolled to the top after navigating to a URL.
253
When the user clicks the browser's back button, the previous scroll position will be maintained.
259
<td><code>timeout</code></td>
264
Time in milliseconds after which an Ajax request should time out. When a timeout occurs, the <code>error</code> event will be fired.
270
<td><code>titleSelector</code></td>
272
<td><code>"title"</code></td>
275
CSS selector used to extract a page title from the content of a page loaded via Pjax.
279
By default this is set to extract the title from the <code><title></code> element, but you could customize it to extract the title from an <code><h1></code>, or from any other element, if that's more appropriate for the content you're loading.
286
<h4 id="custom-selectors">Custom Selectors</h4>
289
Pjax's <code>contentSelector</code> and <code>titleSelector</code> config attributes allow you to customize how content and page titles are extracted from loaded pages, while the <code>linkSelector</code> attribute lets you customize which links on the page are loaded via Pjax.
292
<h5 id="contentselector"><code>contentSelector</code></h5>
295
By default, <code>contentSelector</code> is <code>null</code>, meaning that the entire contents of any loaded page will be inserted into the container node. This could be bad if the page includes a header and footer that shouldn't be displayed again. Specify a custom <code>contentSelector</code> to display only a portion of the loaded page.
299
Let's say we have an HTML page that looks like this:
302
<pre class="code prettyprint"><!DOCTYPE html>
303
<title>The Surprising Adventures of Sir Digby Chicken Caesar - Episode 1</title>
305
<div class="header">
306
<img src="header.jpg" alt="Sir Digby and his companion Ginger">
307
<h1>The Surprising Adventures of Sir Digby Chicken Caesar</h1>
310
<div class="episode-content">
311
<h2>Episode One</h2>
314
On a lonely planet spinning its way toward damnation amid the fear and
315
despair of a broken human race, who is left to fight for all that is
316
good and pure and gets you smashed for under a fiver? Yes, it's the
317
surprising adventures of me, Sir Digby Chicken Caesar!
321
<div class="footer">
323
Copyright (c) 2012 Digby Enterprises.
326
</body></pre>
330
Since the header and footer are persistent across the site, we only want to display the content portion when this page is loaded via Pjax. All we need to do is set the <code>contentSelector</code> attribute to the appropriate CSS selector:
333
<pre class="code prettyprint">// Extract the contents of the '.episode-content' div from loaded pages and
334
// discard the rest.
336
container: '#content',
337
contentSelector: '.episode-content'
341
<h5 id="linkselector"><code>linkSelector</code></h5>
344
The <code>linkSelector</code> attribute allows you to customize which links Pjax will handle. By default, <code>linkSelector</code> is set to "a.yui3-pjax", which means that any <code><a></code> element with the class name "yui3-pjax" will be handled by Pjax. You could customize this to change the class name, or to limit Pjax to links inside a certain container, or anything else you can do with a CSS selector.
347
<h5 id="titleselector"><code>titleSelector</code></h5>
350
The <code>titleSelector</code> attribute allows you to customize how Pjax extracts a page title from loaded pages. By default, it's set to the selector string "title", which means it will extract the contents of the first <code><title></code> element on the loaded page. If desired, you could customize this selector to extract the title from an <code><h1></code> element, or any other element, or set it to <code>null</code> to prevent a title from being extracted.
353
<h3 id="pjax-events">Pjax Events</h3>
366
<td><code>error</code></td>
369
Fired when an error occurs while attempting to load a URL.
374
<dt><code>content</code> <em>(Object)</em></dt>
377
Content extracted from the response, if any. This is an object with the following properties:
381
<dt><code>node</code> <em>(Node)</em></dt>
383
A <code>Y.Node</code> instance for a document fragment containing the extracted HTML content.
386
<dt><code>title</code> <em>(String)</em></dt>
388
The title of the HTML page, if any, extracted using the <code>titleSelector</code> attribute. If <code>titleSelector</code> is not set or if a title could not be found, this property will be <code>undefined</code>.
393
<dt><code>responseText</code> <em>(String)</em></dt>
395
Raw Ajax response text.
398
<dt><code>status</code> <em>(Number)</em></dt>
400
HTTP status code for the Ajax response.
403
<dt><code>url</code> <em>(String)</em></dt>
405
The absolute URL that failed to load.
412
<td><code>load</code></td>
415
Fired when a URL is successfully loaded.
420
<dt><code>content</code> <em>(Object)</em></dt>
423
Content extracted from the response, if any. This is an object with the following properties:
427
<dt><code>node</code> <em>(Node)</em></dt>
429
A <code>Y.Node</code> instance for a document fragment containing the extracted HTML content.
432
<dt><code>title</code> <em>(String)</em></dt>
434
The title of the HTML page, if any, extracted using the <code>titleSelector</code> attribute. If <code>titleSelector</code> is not set or if a title could not be found, this property will be <code>undefined</code>.
439
<dt><code>responseText</code> <em>(String)</em></dt>
441
Raw Ajax response text.
444
<dt><code>status</code> <em>(Number)</em></dt>
446
HTTP status code for the Ajax response.
449
<dt><code>url</code> <em>(String)</em></dt>
451
The absolute URL that was loaded.
458
<td><code>navigate</code></td>
461
Fired when navigating to a URL via Pjax. This is a useful event to listen to if you want to add a visual loading indicator while content is loading.
466
<dt><code>force</code> <em>(Boolean)</em></dt>
468
<code>true</code> if enhanced navigation should occur even in browsers that don't support HTML5 history.
471
<dt><code>originEvent</code> <em>(Event)</em></dt>
473
The event that triggered navigation. Usually this will be a DOM click event.
476
<dt><code>replace</code> <em>(Boolean)</em></dt>
478
<code>true</code> if the current history entry will be replaced instead of a new entry being created.
481
<dt><code>url</code> <em>(String)</em></dt>
483
The URL being navigated to.
491
<h2 id="optimizing-server-responses-for-pjax">Optimizing Server Responses for Pjax</h2>
494
While the Pjax Utility is capable of extracting and displaying small portions of loaded pages, it's much more efficient to avoid sending unnecessary content in the first place. With a little extra work on the server side, you can have the server recognize Pjax requests and send back only the relevant portion of the requested page.
497
<h3 id="x-pjax-http-header"><code>X-PJAX</code> HTTP Header</h3>
500
When the Pjax Utility makes an Ajax request to the server, it adds a special <code>X-PJAX</code> HTTP header to the request. You can check for the presence of this header on the server when handling the request to determine whether you should respond with a full page or a partial page.
504
A complete HTTP request generated by the Pjax Utility looks something like this:
508
GET /example.html?pjax=1 HTTP/1.1
510
X-Requested-With: XMLHttpRequest
512
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7
514
Referer: http://example.com/index.html
515
Accept-Encoding: gzip,deflate,sdch
516
Accept-Language: en-US,en;q=0.8
517
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
520
<h3 id="pjax-query-parameter"><code>pjax</code> Query Parameter</h3>
523
When the <code>addPjaxParam</code> config attribute is <code>true</code> (the default), the Pjax Utility will add a <code>pjax=1</code> query parameter to the end of all URLs it requests. This serves two purposes:
528
It provides an alternative to the <code>X-PJAX</code> HTTP header for determining whether a request was generated by the Pjax Utility.
532
It ensures that the browser caches Pjax responses separately from full page responses.
536
<h2 id="known-issues">Known Issues</h2>
541
Pjax's partial page loads only work in browsers that support HTML5 history (most modern browsers do). In older browsers such as IE6 through IE9, pjax-enabled links will result in full page loads. This is by design, since it allows you to take advantage of the functionality supported by modern browsers while providing a graceful fallback for older browsers.
549
<div class="yui3-u-1-4">
550
<div class="sidebar">
552
<div id="toc" class="sidebox">
554
<h2 class="no-toc">Table of Contents</h2>
560
<a href="#getting-started">Getting Started</a>
563
<a href="#using-pjax">Using Pjax</a>
566
<a href="#quick-start">Quick Start</a>
569
<a href="#instantiating-pjax">Instantiating Pjax</a>
572
<a href="#as-a-plugin">As a Plugin</a>
575
<a href="#as-a-class">As a Class</a>
580
<a href="#configuring-pjax">Configuring Pjax</a>
583
<a href="#config-attributes">Config Attributes</a>
586
<a href="#custom-selectors">Custom Selectors</a>
589
<a href="#contentselector"><code>contentSelector</code></a>
592
<a href="#linkselector"><code>linkSelector</code></a>
595
<a href="#titleselector"><code>titleSelector</code></a>
602
<a href="#pjax-events">Pjax Events</a>
607
<a href="#optimizing-server-responses-for-pjax">Optimizing Server Responses for Pjax</a>
610
<a href="#x-pjax-http-header"><code>X-PJAX</code> HTTP Header</a>
613
<a href="#pjax-query-parameter"><code>pjax</code> Query Parameter</a>
618
<a href="#known-issues">Known Issues</a>
633
<script src="../assets/vendor/prettify/prettify-min.js"></script>
634
<script>prettyPrint();</script>