Permalink
Browse files

Redirect on load of a generated hash is detected

  • Loading branch information...
1 parent 91ac2a8 commit 550c3721f445405d6884c4274a637cf89a50d6b0 @fortes committed Aug 10, 2010
Showing with 23 additions and 3 deletions.
  1. +2 −0 README.md
  2. +21 −3 src/history.js
View
@@ -4,6 +4,8 @@ history.js provides an implementation of the [HTML5 history API](http://www.what
Browsers that already support his event (Chrome & Safari, Firefox 4+) use native implementations, which are able to change the page URL without refreshing the page. Older browsers use `location.hash` in order to mimic the official behavior.
+1214 bytes minified / 588 bytes gzipped
+
## Limitations
* Does not support multiple handlers for the `onpopstate` event
View
@@ -29,7 +29,24 @@ if (DEBUG) {
}
}
-window.history_js = window.history_js || { hashInterval: 100 };
+window.history_js = window.history_js || { hashInterval: 100, delimiter: '-' };
+
+// Even if the client has an implementation of the API, we have to check the hash
+// just in case the visitor followed a link generated by a browser that does not
+// support the API
+if (document.location.hash) {
+ (function () {
+ // Sanitize
+ var hash = document.location.hash[0] === '#' ? document.location.hash.substr(1) : document.location.hash;
+
+ // Our hashes always start with the delimiter and have at least another character there
+ if (hash[0] === window.history_js.delimiter && hash.length >= 2) {
+ // Redirect, stripping the intial delimiter
+ hash = hash.substr(1);
+ document.location = hash;
+ }
+ }());
+}
(function (history_js, window, location) {
// Check if the browser already has a native implementation
@@ -38,8 +55,6 @@ window.history_js = window.history_js || { hashInterval: 100 };
console.info('pushState: history.pushState already implemented, skipping');
}
- // TODO: Must redirect if there's a hash, due to legacy
-
// Native implementation present, we are done here
return;
}
@@ -87,6 +102,9 @@ window.history_js = window.history_js || { hashInterval: 100 };
* @param {boolean} replace
*/
function changeState (data, title, url, replace) {
+ // Always add delimiter and escape hash
+ url = history_js.delimiter + escape(url);
+
// Store data using url
history_js.setStorage(url, { state: data, title: title });

0 comments on commit 550c372

Please sign in to comment.