Created
August 3, 2017 14:18
-
-
Save himalay/e0e2728bcc33d822cca6bd75ef1a566d to your computer and use it in GitHub Desktop.
Hash routing in js
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
(function() { | |
var Router = { | |
root: '#/', | |
routes: [], | |
urls: [], | |
titles: [], | |
add: function(thePath, theUrl, theTitle) { | |
this.routes.push(thePath); | |
this.urls.push(theUrl); | |
this.titles.push(theTitle); | |
}, | |
navigate: function() { | |
var routes = this.routes, | |
urls = this.urls, | |
root = this.root; | |
function loading() { | |
var a = $.inArray(location.hash, routes), | |
template = urls[a]; | |
if (a === -1) { | |
location.hash = root; | |
$("#view").load(urls[0]); | |
} | |
else { | |
$("#view").load(template); | |
if (a === 0) { | |
window.scrollTo(0, 0); | |
} | |
else { | |
window.scrollTo(0, 90); | |
} | |
} | |
} | |
window.onload = loading; | |
window.onhashchange = loading; | |
} | |
}; | |
Router.add("#/", "tpl/home.html", "Home Page"); | |
Router.add("#/about", "tpl/about.html", "About Page"); | |
Router.add("#/licence", "tpl/licence.html", "MIIT Licence"); | |
Router.add("#/cabala", "tpl/cabala.html", "Cabala Checker"); | |
Router.add("#/articles/esp", "tpl/article1.html", "ESP"); | |
Router.add("#/fanfics/the-chupacabra-case", "tpl/article2.html", "The Chupacabra Case"); | |
Router.navigate(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment