A standalone implementation of push state AJAX, designed for use on non-JQuery web pages. The design is loosely based on the original jQuery implementation found at: https://github.com/defunkt/jquery-pjax
This code is licensed under the MIT License.
This code has been tested in Chrome, Firefox, Opera and IE7, 8, 9 and 10. PJAX is supported in Chrome, Firefox, IE10+ and Opera, while in older IE versions the fall backs operate as expected.
A live version of the demo can be viewed here: http://userbag.co.uk/demo/pjax/
To use PJAX on your page you will need to include the pjax-standalone.js script in to the head of the page. Alternately, if the assets for your site are built (using a tool like grunt) PJAX standalone can also be installed using bower bower install pjax-standalone
.
Once done, PJAX can be setup in 3 ways.
Give all links a data-pjax attribute specifying where to place the content that gets loaded:
<a href='page1.php' data-pjax='content'>Page 1</a>
Then call:
pjax.connect();
Add links normally
<a href='page2.php'>Page 2</a>
Then specify which container they should use, via either
pjax.connect('content');
or
pjax.connect({container: 'content'});
Set all links with a specific class to use a particular container using:
<a href='page2.php' class='pjaxer'>Page 2</a>
pjax.connect('content', 'pjaxer');
The PJAX connect method supports the following options:
PJAX-Standalone implements the following callbacks/events:
The callbacks can be specified either as part of the original pjax.connect method:
pjax.connect({
'container': 'content',
'beforeSend': function(e){ console.log("before send"); },
'complete': function(e){ console.log("done!"); },
});
Or by adding your own listener to the specified container
document.getElementById("my_container").addEventListener('complete', function(event){ console.log(event); }, false);
The PJAX options at the time of an event being triggered can be accessed via event.data
You can invoke a PJAX page load programmatically by calling the pjax.invoke()
method.
At minimum the PJAX invoke method must be given a URL
and container
attribute. It can also
be provided with a title
, along with any other standard config item or callback you may wish.
pjax.invoke({url:'page1.php', 'container': 'content'});
or
pjax.invoke('page1.php', 'content');
Update your code to return only the main content area when the X-PJAX header is set, while returning the full website layout when it is not.
<?php
if(isset($_SERVER['HTTP_X_PJAX']) && $_SERVER['HTTP_X_PJAX'] == 'true'){
// Output content on its own.
}else{
// Output content with wrapper/layout
}
If you are unable to change the server side code or simply do not want to, So long as smartLoad is enabled (which it is by default), PJAX-Standalone will extract the container_divs content from the returned HTML and apply it to the current page meaning PJAX loading will still work as expect (although some of PJAX's performance gains may be lost).