Skip to content

Commit

Permalink
[fixed] Using HashLocation without a preceeding /
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Aug 27, 2014
1 parent a63c940 commit 25adcab
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions modules/locations/HashLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,28 @@ var invariant = require('react/lib/invariant');
var ExecutionEnvironment = require('react/lib/ExecutionEnvironment');
var getWindowPath = require('../helpers/getWindowPath');

function getHashPath() {
return window.location.hash.substr(1);
}

function ensureSlash() {
var path = getHashPath();

if (path.charAt(0) === '/')
return true;

HashLocation.replace('/' + path);

return false;
}

var _onChange;

function handleHashChange() {
if (ensureSlash())
_onChange();
}

/**
* A Location that uses `window.location.hash`.
*/
Expand All @@ -17,22 +37,20 @@ var HashLocation = {

_onChange = onChange;

// Make sure the hash is at least / to begin with.
if (window.location.hash === '')
window.location.replace(getWindowPath() + '#/');
ensureSlash();

if (window.addEventListener) {
window.addEventListener('hashchange', _onChange, false);
window.addEventListener('hashchange', handleHashChange, false);
} else {
window.attachEvent('onhashchange', _onChange);
window.attachEvent('onhashchange', handleHashChange);
}
},

teardown: function () {
if (window.removeEventListener) {
window.removeEventListener('hashchange', _onChange, false);
window.removeEventListener('hashchange', handleHashChange, false);
} else {
window.detachEvent('onhashchange', _onChange);
window.detachEvent('onhashchange', handleHashChange);
}
},

Expand All @@ -48,9 +66,7 @@ var HashLocation = {
window.history.back();
},

getCurrentPath: function () {
return window.location.hash.substr(1);
},
getCurrentPath: getHashPath,

toString: function () {
return '<HashLocation>';
Expand Down

0 comments on commit 25adcab

Please sign in to comment.