• Stars
    star
    203
  • Rank 192,282 (Top 4 %)
  • Language
    JavaScript
  • Created over 12 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A standalone implementation of Pushstate AJAX, for non-jquery webpages.

PJAX-Standalone#

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/

Usage Instructions

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.

Option 1

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();

Option 2

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'});

Option 3

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');

Options

The PJAX connect method supports the following options:

  • useClass - string - Apply PJAX only to links with the provided class.
  • excludeClass - string - If set, PJAX will ignore any link containing the class
  • parseLinksOnload - true|false - Make links in loaded pages use PJAX. Enabled by default.
  • smartLoad - true|false - Ensure returned HTML is correct. Enabled by default.
  • autoAnalytics - true|false - Enabled by default, will attempt to automatically track page views to any detected Google analytics trackers.
  • returnToTop - true|false - Enabled by default, scrolls browser window to top of page, when new content is loaded.
  • parseJS - true|false - Disabled by default, attempt to execute JavaScript found within pages load via PJAX. (plain JS will be run each time the page loads, external files will only be loaded the first time.)
  • ignoreFileTypes - array of file types to be ignored by PJAX. By default this includes PDF, ZIP and a number of other common non-pjax loadable types.

Callbacks

PJAX-Standalone implements the following callbacks/events:

  • beforeSend - Called before AJAX request is made
  • complete - When AJAX request has completed
  • success - When AJAX request has completed successfully
  • error - When AJAX request did not complete successfully (error 404/500 etc)
  • ready - Fired when PJAX completes initial link parsing

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

Using PJAX-Standalone programmatically

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');

Server side.

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

More Repositories

1

PHP-SharePoint-Lists-API

A simple PHP API to make working with SharePoint lists easy.
PHP
181
star
2

JavaScript-Ical-Parser

Proof of concept Javascript ical (.ics) parser
JavaScript
39
star
3

PHP-Secret-Santa

A simple PHP Secret Santa Script
PHP
15
star
4

JSnip

JSnip: A Lightweight JavaScript Snippeting package built using base.js
JavaScript
6
star
5

UrlParse

A simple JavaScript URL Parsing Utility
JavaScript
5
star
6

Story-Card

A highly customisable interactive Story Card dashboard.
PHP
5
star
7

bonus-laravel-relations

A selection of weird & wonderful additional relation types for laravel's eloquent ORM
PHP
5
star
8

theProfile

TheProfile is an open source, multi-user online identity managment tool, designed to allow users to manage a single consistent identity accross a range of websites.
PHP
5
star
9

Model-Uni

An open source university simulation game
JavaScript
4
star
10

RPGCampaignManager

An Opensource RPG Campaign Manager and World Building tool.
PHP
4
star
11

yare-bot

A bot for yare.io
JavaScript
4
star
12

quick-spot

Quickspot is a fast, flexible JSON powered in-memory search.
JavaScript
4
star
13

HTML5-JavaScript-Game-Demos-Tests

A holding place of JavaScript/HTML5 Demo's and tests i create.
JavaScript
3
star
14

rpg-quick-encounter

RPG quick encounter is an online tool to help you get up and running with your next encounter in moments.
JavaScript
3
star
15

PseudoModel

PseudoModel is an eloquent-like base model for laravel
PHP
2
star
16

Magic-2.0

A Lightweight PHP5 MVC Framework [ Note: This product is not yet in a usable state]
PHP
2
star
17

base.js

A simple standalone library of useful JavaScript methods
JavaScript
1
star
18

json-api-rehydrate

A JS utility for turning a JSON:API response back in to a hierarchical data structure
JavaScript
1
star
19

thybag

Profile readme
1
star
20

FormZ

A Simple Stand-alone PHP Form Helper
PHP
1
star