Skip to content

Commit 25adcab

Browse files
committed
[fixed] Using HashLocation without a preceeding /
Fixes #230
1 parent a63c940 commit 25adcab

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

modules/locations/HashLocation.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,28 @@ var invariant = require('react/lib/invariant');
22
var ExecutionEnvironment = require('react/lib/ExecutionEnvironment');
33
var getWindowPath = require('../helpers/getWindowPath');
44

5+
function getHashPath() {
6+
return window.location.hash.substr(1);
7+
}
8+
9+
function ensureSlash() {
10+
var path = getHashPath();
11+
12+
if (path.charAt(0) === '/')
13+
return true;
14+
15+
HashLocation.replace('/' + path);
16+
17+
return false;
18+
}
19+
520
var _onChange;
621

22+
function handleHashChange() {
23+
if (ensureSlash())
24+
_onChange();
25+
}
26+
727
/**
828
* A Location that uses `window.location.hash`.
929
*/
@@ -17,22 +37,20 @@ var HashLocation = {
1737

1838
_onChange = onChange;
1939

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

2442
if (window.addEventListener) {
25-
window.addEventListener('hashchange', _onChange, false);
43+
window.addEventListener('hashchange', handleHashChange, false);
2644
} else {
27-
window.attachEvent('onhashchange', _onChange);
45+
window.attachEvent('onhashchange', handleHashChange);
2846
}
2947
},
3048

3149
teardown: function () {
3250
if (window.removeEventListener) {
33-
window.removeEventListener('hashchange', _onChange, false);
51+
window.removeEventListener('hashchange', handleHashChange, false);
3452
} else {
35-
window.detachEvent('onhashchange', _onChange);
53+
window.detachEvent('onhashchange', handleHashChange);
3654
}
3755
},
3856

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

51-
getCurrentPath: function () {
52-
return window.location.hash.substr(1);
53-
},
69+
getCurrentPath: getHashPath,
5470

5571
toString: function () {
5672
return '<HashLocation>';

0 commit comments

Comments
 (0)