Skip to content

Commit 41ea0e6

Browse files
committed
⬆️ 2.6.0 🎉
1 parent acf4e96 commit 41ea0e6

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

lib/index.js

+31-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const Url = module.exports = {
66

77
_onPopStateCbs: []
88
, _isHash: false
9+
, current_location: ""
10+
, previous_location: ""
911

1012
/**
1113
* queryString
@@ -172,10 +174,11 @@ const Url = module.exports = {
172174
*
173175
* @name getLocation
174176
* @function
177+
* @param {Boolean} excludeHash If `true`, the location hash will not be appended in the result.
175178
* @return {String} The page url (without domain).
176179
*/
177-
, getLocation () {
178-
return window.location.pathname + window.location.search + window.location.hash;
180+
, getLocation (excludeHash) {
181+
return window.location.pathname + window.location.search + (excludeHash ? "" : window.location.hash);
179182
}
180183

181184
/**
@@ -198,7 +201,12 @@ const Url = module.exports = {
198201
}, 0);
199202
Url._isHash = true;
200203
}
201-
return location.hash = newHash;
204+
205+
206+
location.hash = newHash;
207+
Url.previous_location = Url.current_location
208+
Url.current_location = Url.getLocation(true)
209+
return newHash
202210
}
203211

204212
/**
@@ -215,6 +223,10 @@ const Url = module.exports = {
215223
* @return {String} The set url.
216224
*/
217225
, _updateAll (s, push, triggerPopState) {
226+
if (push) {
227+
Url.previous_location = Url.current_location
228+
}
229+
Url.current_location = Url.getLocation(true)
218230
window.history[push ? "pushState" : "replaceState"](null, "", s);
219231
if (triggerPopState) {
220232
Url.triggerPopStateCb({});
@@ -250,6 +262,19 @@ const Url = module.exports = {
250262
* @function
251263
*/
252264
, triggerPopStateCb (e) {
265+
266+
setTimeout(function () {
267+
Url.current_location = Url.getLocation(true)
268+
}, 0)
269+
270+
if (Url.previous_location === Url.current_location) {
271+
Url._isHash = true
272+
setTimeout(function () {
273+
Url._isHash = false
274+
window.history.back()
275+
}, 0);
276+
return
277+
}
253278
if (this._isHash) { return; }
254279
this._onPopStateCbs.forEach(function (c) {
255280
c(e)
@@ -298,5 +323,7 @@ const Url = module.exports = {
298323
this._updateAll(window.location.pathname + window.location.hash, push || false, trigger || false);
299324
}
300325

301-
, version: "2.5.0"
326+
, version: "2.6.0"
302327
};
328+
329+
Url.current_location = Url.getLocation(true)

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "urljs",
3-
"version": "2.5.0",
3+
"version": "2.6.0",
44
"description": "A lightweight JavaScript library to manipulate the page url.",
55
"main": "lib/index.js",
66
"directories": {
@@ -89,4 +89,4 @@
8989
}
9090
]
9191
}
92-
}
92+
}

0 commit comments

Comments
 (0)