Last active
August 29, 2015 13:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
********* | |
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