Skip to content

[fixed] Using HashLocation without a preceeding / #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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