Created
December 5, 2011 17:05
-
-
Save janlelis/1434347 to your computer and use it in GitHub Desktop.
pagination vs. permalinks
This file contains 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
<script type="text/javascript"> | |
var pagination = function(){ | |
var hash = window.location.hash.substr(1); | |
if(hash && !document.getElementById(hash)){ | |
var per_page = {{ paginator.per_page }}; | |
var post_ids = [ | |
{% for post in site.posts %} | |
"{{ post.id | replace:'"','\"' }}", | |
{% endfor %} | |
]; | |
var offset = Math.floor(post_ids.indexOf(hash) / per_page) + 1; | |
var redirect_to; | |
if(offset > 0){ // found | |
if(offset === 1){ | |
redirect_to = "/#" + hash | |
} | |
else{ | |
redirect_to = "/page" + offset + "/#" + hash | |
} | |
location.href = location.protocol + "//" + location.host + redirect_to; | |
} | |
} | |
} | |
pagination(); | |
window.onhashchange = pagination; // this might not work for older browsers | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment