Skip to content

Instantly share code, notes, and snippets.

@abahler
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abahler/10443001 to your computer and use it in GitHub Desktop.
Save abahler/10443001 to your computer and use it in GitHub Desktop.
First-time use of YUI's "pjax" module to load HTML content into part of a page without a refresh
/*
*********
pjax.js
Author: Alex Bahler
Date: April 10, 2014
*********
*/
YUI().use("node", "pjax", function(y){
// Link to appropriate index.html page, based on where user is in the site
var here = window.location.href;
var url;
// Load top-level index page if user is on "/careers/" or "/contact/" pages
if (here.indexOf("careers") >= 0 || here.indexOf("contact") >= 0) {
url = "../index.html";
// "/clients/" sub-directory has its own index page
} else if (here.indexOf("clients") >= 0) {
url = "index.html";
}
// Create pjax link and insert before div#wrapper closes
var homeLink = y.Node.create('<a href="' + url + '" id="pjax-link" class="yui3-pjax">Load Home Page Content</a>');
homeLink.appendTo("#wrapper");
var pjax = new y.Pjax({
// NOTE: Having 'container' parameter only, and linking to "[../]index.html #wrapper", results in 404
// Load into this page's #wrapper...
container: "#wrapper",
// ...only the content within the destination page's #wrapper
contentSelector: "#wrapper"
});
// Confirm everything worked
pjax.on({
"load": function(e) {
console.log("Success!");
},
"error": function(e) {
console.log("There was an error: " + e.responseText);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment