-
Notifications
You must be signed in to change notification settings - Fork 0
/
iURL.js
executable file
·117 lines (97 loc) · 3.55 KB
/
iURL.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(function (window, document, root) {
"use strict";
/**
* @author Knyazevich Denis
* @type {*}
*/
var iURL = {
/**
*
* @param name
* @returns {*}
*/
get: function (name) {
if (!name || !document.location.search) {
return null;
}
name = name.toLowerCase();
var _stringWithParams = (document.location.search.toLowerCase().replace(/^\?/, '')),
_params = _stringWithParams.split('&'),
result = null,
i,
len = _params.length;
for (i = 0; i < len; i++) {
if (typeof _params[i] === 'string') {
var _data = _params[i].split('=');
if (_data[0] == name) {
result = _data[1] || null;
break;
}
}
}
return result;
},
/**
* есть ли get-параметры
*/
hasParams: function () {
return !!document.location.search.replace(/^\?/, '');
},
/**
*
* @returns {{}}
*/
getSearchParams: function () {
var data = {},
searchString,
searchParamsSection,
i,
len,
searchParamsData;
if (document.location.search) {
searchString = document.location.search.replace(/^\?/, '').replace(/\&$/, '');
searchParamsSection = searchString.split("&");
len = searchParamsSection.length;
for (i = 0; i < len; i++) {
searchParamsData = searchParamsSection[i].split("=");
data[searchParamsData[0]] = searchParamsData[1] ? decodeURIComponent(searchParamsData[1]) : "";
}
}
return data;
},
replaceSearchParams: function (url, params) {
var i,
name,
otherParams = [],
paramsAsSearchString = '',
searchQuerySplitter = '?',
searchParamsSplitter = "&",
searchDataSplitter = "=",
searchString = '',
urlData = url.split(searchQuerySplitter),
hasSearchParams = !!urlData[1],
searchData = (urlData[1] || "").split(searchParamsSplitter),
len = searchData.length;
for (i in params) {
if (params.hasOwnProperty(i)) {
paramsAsSearchString += i + searchDataSplitter + params[i] + "&";
}
}
paramsAsSearchString = paramsAsSearchString.replace(/\&$/, '');
if (!hasSearchParams) {
searchString = paramsAsSearchString;
} else {
for (i = len - 1; i >= 0; i--) {
name = searchData[i].split(searchDataSplitter)[0];
if (params[name] === undefined) {
otherParams.push(searchData[i]);
}
}
otherParams.push(paramsAsSearchString);
searchString = otherParams.join(searchParamsSplitter);
}
return urlData[0] + searchQuerySplitter + searchString;
}
};
(root || window).iURL = iURL;
}(window, document, this));