Skip to content

Instantly share code, notes, and snippets.

@sbp
Created August 20, 2011 11:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbp/1158979 to your computer and use it in GitHub Desktop.
Save sbp/1158979 to your computer and use it in GitHub Desktop.
jQuery Paginator script
/*!
* jQuery Paginator v1.0 - 20 Aug 2011
* Copyright 2011 Sean B. Palmer, Apache License 2.0
* http://sbp.so/paginator
*/
$(function () {
var body = $('body');
var content = false;
function restore_ids(elements) {
$('[data-id]', elements).each(function() {
var e = $(this);
e.attr('id', e.attr('data-id')).removeAttr('data-id');
});
}
function restore_content() {
body.append(content.children());
content = false;
}
$(window).hashchange(function() {
if (content || location.hash)
{
if (!content) // -> page, body -> page
{
// Only restore_ids for body -> page really
// But we can't detect body -> page by itself easily
restore_ids(body);
var page = $(location.hash, body);
if (!page.size()) return;
content = $('<div>').append(body.children().detach());
body.append(page.clone(true).removeAttr('id'));
}
else if (location.hash) // page -> page
{
restore_ids(content);
body.empty();
var page = $(location.hash, content);
if (!page.size()) return restore_content();
body.append(page.clone(true).removeAttr('id'));
}
else // page -> body
{
body.empty();
restore_content();
}
} // else, -> body
});
$('a[href^=#]').click(function() {
var hash = $(this).attr('href');
$(hash).attr('data-id', hash.substring(1)).removeAttr('id');
});
$(window).hashchange();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment