diff --git a/lib/index.js b/lib/index.js index 6632d1d..095d1d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,6 +6,8 @@ const Url = module.exports = { _onPopStateCbs: [] , _isHash: false + , current_location: "" + , previous_location: "" /** * queryString @@ -172,10 +174,11 @@ const Url = module.exports = { * * @name getLocation * @function + * @param {Boolean} excludeHash If `true`, the location hash will not be appended in the result. * @return {String} The page url (without domain). */ - , getLocation () { - return window.location.pathname + window.location.search + window.location.hash; + , getLocation (excludeHash) { + return window.location.pathname + window.location.search + (excludeHash ? "" : window.location.hash); } /** @@ -198,7 +201,12 @@ const Url = module.exports = { }, 0); Url._isHash = true; } - return location.hash = newHash; + + + location.hash = newHash; + Url.previous_location = Url.current_location + Url.current_location = Url.getLocation(true) + return newHash } /** @@ -215,6 +223,10 @@ const Url = module.exports = { * @return {String} The set url. */ , _updateAll (s, push, triggerPopState) { + if (push) { + Url.previous_location = Url.current_location + } + Url.current_location = Url.getLocation(true) window.history[push ? "pushState" : "replaceState"](null, "", s); if (triggerPopState) { Url.triggerPopStateCb({}); @@ -250,6 +262,19 @@ const Url = module.exports = { * @function */ , triggerPopStateCb (e) { + + setTimeout(function () { + Url.current_location = Url.getLocation(true) + }, 0) + + if (Url.previous_location === Url.current_location) { + Url._isHash = true + setTimeout(function () { + Url._isHash = false + window.history.back() + }, 0); + return + } if (this._isHash) { return; } this._onPopStateCbs.forEach(function (c) { c(e) @@ -298,5 +323,7 @@ const Url = module.exports = { this._updateAll(window.location.pathname + window.location.hash, push || false, trigger || false); } - , version: "2.5.0" + , version: "2.6.0" }; + +Url.current_location = Url.getLocation(true) diff --git a/package.json b/package.json index 67d0c68..737c295 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "urljs", - "version": "2.5.0", + "version": "2.6.0", "description": "A lightweight JavaScript library to manipulate the page url.", "main": "lib/index.js", "directories": { @@ -89,4 +89,4 @@ } ] } -} +} \ No newline at end of file