summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2015-03-04 17:02:38 +0100
committerPeter Wu <peter@lekensteyn.nl>2015-03-04 17:02:38 +0100
commita76caf717fd2c60f30bc62baa2522a16e4467268 (patch)
tree5f2d8bf0936231a40ca98e84bd6191a6e1a3eed6 /js
downloadsite-a76caf717fd2c60f30bc62baa2522a16e4467268.tar.gz
Initial commit
Diffstat (limited to 'js')
-rw-r--r--js/main.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/js/main.js b/js/main.js
new file mode 100644
index 0000000..5e6816e
--- /dev/null
+++ b/js/main.js
@@ -0,0 +1,39 @@
+/* jshint browser:true, devel:true */
+(function() {
+ 'use strict';
+
+ /* loads a page into view and execute scripts */
+ function loadContent(name, text) {
+ var contentElement = document.getElementById('content');
+ contentElement.innerHTML = text;
+ document.body.dataset.page = name;
+ }
+
+ /* tries to navigate to a page */
+ function selectPage(page) {
+ var pages = 'home studying curriculum campus career'.split(' ');
+ if (pages.indexOf(page) == -1) {
+ // TODO: 404
+ console.log("404 " + page);
+ return false;
+ }
+ console.log("Loading " + page);
+ var http = new XMLHttpRequest();
+ http.onload = function() {
+ loadContent(page, http.responseText);
+ };
+ http.open('get', 'pages/' + page + '.html');
+ http.send(null);
+ return true;
+ }
+
+ addEventListener('hashchange', function(ev) {
+ var m = /#\/(.+)/.exec(ev.newURL);
+ if (!m)
+ return;
+ selectPage(m[1]);
+ });
+
+ // Tries to load the current page, falling back to "home" for unknown URLs.
+ selectPage(location.hash.replace(/^#\//, '')) || selectPage('home');
+})();