-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl-parser.js
71 lines (52 loc) · 1.94 KB
/
url-parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// url parser
// thx http://james.padolsey.com/javascript/parsing-urls-with-the-dom
// thx https://davidwalsh.name/get-absolute-url
// updated ^bg @2018, @2017, @2013
function url_parameterByName_get(url, name) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
var results = regex.exec(url);
if (!results) {
return null;
}
if (!results[2]) {
return '';
}
var ret = decodeURIComponent(results[2].replace(/\+/g, " "));
return ret;
}
function url_parse(url) {
var a = document.createElement('a');
a.href = url;
var parsedObject = {
source: url,
pathName: a.pathname,
absoluteUrl: a.pathname,
protocol: a.protocol.replace(':',''),
domainHost: url.split('/')[2],
domain: url.split('/')[2].split('.')[2] ? url.split('/')[2].split('.')[1] + '.' + url.split('/')[2].split('.')[2] : url.split('/')[2],
domainTld: url.split('/')[2].split('.')[2] ? url.split('/')[2].split('.')[2] : url.split('/')[2].split('.')[1],
port: a.port,
query: a.search,
params: (function() {
var ret = {},
seg = a.search.replace(/^\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash,//.replace('#',''),
path: a.pathname.replace(/^([^\/])/,'/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^\//,'').split('/')
};
return parsedObject;
}