diff --git a/lib/environment/PathnameEnvironment.js b/lib/environment/PathnameEnvironment.js index 840eaa1..669b87c 100644 --- a/lib/environment/PathnameEnvironment.js +++ b/lib/environment/PathnameEnvironment.js @@ -7,6 +7,9 @@ var Environment = require('./Environment'); */ function PathnameEnvironment() { this.onPopState = this.onPopState.bind(this); + this.useHistoryApi = !!(window.history && + window.history.pushState && + window.history.replaceState); Environment.call(this); } @@ -18,21 +21,29 @@ PathnameEnvironment.prototype.getPath = function() { } PathnameEnvironment.prototype.pushState = function(path, navigation) { - window.history.pushState({}, '', path); + if (this.useHistoryApi) { + window.history.pushState({}, '', path); + } else { + window.location.pathname = path; + } } PathnameEnvironment.prototype.replaceState = function(path, navigation) { - window.history.replaceState({}, '', path); + if (this.useHistoryApi) { + window.history.replaceState({}, '', path); + } else { + window.location.pathname = path; + } } PathnameEnvironment.prototype.start = function() { - if (window.addEventListener) { + if (this.useHistoryApi && window.addEventListener) { window.addEventListener('popstate', this.onPopState); } }; PathnameEnvironment.prototype.stop = function() { - if (window.removeEventListener) { + if (this.useHistoryApi && window.removeEventListener) { window.removeEventListener('popstate', this.onPopState); } };