From a0725576275ce87d5b873fd0cece1c0a8013948c Mon Sep 17 00:00:00 2001 From: Mike Taylor Date: Fri, 1 May 2015 17:27:49 +0100 Subject: [PATCH] First part of parsing hash fragment. --- src/mkws-core.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/mkws-core.js b/src/mkws-core.js index e34676c..deb78d0 100644 --- a/src/mkws-core.js +++ b/src/mkws-core.js @@ -686,7 +686,28 @@ mkws.info("Using window.name '" + window.name + "'"); } } - $(window).bind( 'hashchange', function() { - mkws.warn("hashchange") + // This function is adapted from Yarin's code at + // https://stackoverflow.com/questions/4197591/parsing-url-hash-fragment-identifier-with-javascript#4198132 + function parse_fragment(s) { + var hashParams = {}; + var e, + a = /\+/g, // Regex for replacing addition symbol with a space + r = /([^&;=]+)=?([^&;]*)/g, + d = function (s) { return decodeURIComponent(s.replace(a, " ")); }, + q = s.substring(1); + + while (e = r.exec(q)) + hashParams[d(e[1])] = d(e[2]); + + return hashParams; + } + + var oldHash = location.hash; + $(window).bind('hashchange', function(e) { + mkws.warn("hashchange: old='" + oldHash + "', new='" + location.hash + "'"); + var oldStates = parse_fragment(oldHash); + var newStates = parse_fragment(location.hash); + mkws.warn(mkws.$.toJSON(newStates)); + oldHash = location.hash; }); })(mkws.$); -- 1.7.10.4