-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhref2json.js
56 lines (48 loc) · 1.25 KB
/
href2json.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
/**
Simple parser for document.location
Copyright (c) Alex Vedyakov 2012
MIT Licensed
**/
(function(){
var href = {}
function getPars(string){
var obj = {};
if (string.length>0){
var arr = string.split(/&/g);
if (arr.length>0){
var s1, s2, name, value;
for (var i=0, length=arr.length; i<length; i++){
s1 = arr[i].split('=');
if ( s1.length>1){
name = s1[0];
value = s1[1];
value = decodeURIComponent(value.replace(/\+/g," "));
obj[s1[0]] = s1[1];
}
}
}
}
return obj;
}
href.full = '' + document.location.href;
href.protocol = '' + document.location.protocol;
href.host = '' + document.location.host;
href.hostname = '' + document.location.hostname;
href.port = '' + document.location.port;
href.pathname = '' + document.location.pathname;
href.paths = href.pathname.split('/');
if ( href.paths[0].length == 0 ){
href.paths.shift();
}
href.hash = '' + document.location.hash;
if ( href.hash[0] == '#' ){
href.hash = href.hash.replace('#','');
}
href.hashs = getPars(href.hash);
href.search = '' + document.location.search;
if ( href.search[0] == '?' ){
href.search = href.search.replace('?','');
}
href.searchs = getPars(href.search);
window.href2json = href;
})();