Skip to content

Instantly share code, notes, and snippets.

@everettcaleb
Last active December 23, 2015 03:01
Show Gist options
  • Save everettcaleb/fb74ae1bacea02e52870 to your computer and use it in GitHub Desktop.
Save everettcaleb/fb74ae1bacea02e52870 to your computer and use it in GitHub Desktop.
Fast Navigation Library that requests just the page main-container for common pages
// fastnav.js
// Created by Caleb Everett
// Copyright (c) 2015 Caleb Everett.
// Licensed under MIT License
!function() {
var $mc = $('#main-container'),
requestViaAjax = function(url) {
$.blockUI();
$.ajax({
type: 'GET',
url: url,
success: function(response) {
$mc.html(
$.parseHTML(response).find(function(i) {
return i.id == 'main-container';
})
);
},
error: function(xhr, status, err) {
alert(`${status}: ${xhr.responseText}`);
},
complete: function(xhr, status) {
$.unblockUI();
}
});
};
$(document).on('popstate.fastnav', function() {
requestViaAjax(location.href);
});
$(document).on('click.fastnav', '.fastnav', function(e) {
e.preventDefault();
history.pushState(null, '', e.target.href);
requestViaAjax(e.target.href);
});
}();
// fastnav2.js
// Created by Caleb Everett
// Copyright (c) 2015 Caleb Everett.
// Licensed under MIT License
function FastNav2(linkSelector, mainContainerId) {
var $mc = $(mainContainerId),
requestViaAjax = function(url) {
$.blockUI();
$.ajax({
type: 'GET',
url: url,
success: function(response) {
$mc.html(
$.parseHTML(response).find(function(i) {
return i.id == 'main-container';
})
);
},
error: function(xhr, status, err) {
alert(`${status}: ${xhr.responseText}`);
},
complete: function(xhr, status) {
$.unblockUI();
}
});
};
$(document).on('popstate.fastnav2', function() {
requestViaAjax(location.href);
});
$(document).on('click.fastnav2', linkSelector, function(e) {
e.preventDefault();
history.pushState(null, '', e.target.href);
requestViaAjax(e.target.href);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment