diff --git a/backbone.js b/backbone.js index 29c5d1f9..6411af69 100644 --- a/backbone.js +++ b/backbone.js @@ -965,9 +965,12 @@ this.history = options && options.history || root.history; }; - // Cached regex for cleaning leading hashes and slashes . + // Cached regex for cleaning leading hashes and slashes. var routeStripper = /^[#\/]/; + // Cached regex for stripping a leading and trailing slash. + var rootStripper = /^\/|\/$/g; + // Cached regex for detecting MSIE. var isExplorer = /msie [\w.]+/; @@ -997,7 +1000,7 @@ if (fragment == null) { if (this._hasPushState || !this._wantsHashChange || forcePushState) { fragment = this.location.pathname; - var root = this.options.root.replace(trailingSlash, ''); + var root = this.root.replace(trailingSlash, ''); if (!fragment.indexOf(root)) fragment = fragment.substr(root.length); } else { fragment = this.getHash(); @@ -1015,6 +1018,7 @@ // Figure out the initial configuration. Do we need an iframe? // Is pushState desired ... is it available? this.options = _.extend({}, {root: '/'}, this.options, options); + this.root = this.options.root; this._wantsHashChange = this.options.hashChange !== false; this._wantsPushState = !!this.options.pushState; this._hasPushState = !!(this.options.pushState && this.history && this.history.pushState); @@ -1022,8 +1026,8 @@ var docMode = document.documentMode; var oldIE = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7)); - // Normalize root to always include trailing slash - if (!trailingSlash.test(this.options.root)) this.options.root += '/'; + // Normalize root to always include a leading and trailing slash. + this.root = '/' + this.root.replace(rootStripper, '') + '/'; if (oldIE && this._wantsHashChange) { this.iframe = Backbone.$('