-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyMethods.js
More file actions
86 lines (78 loc) · 2.4 KB
/
Copy pathmyMethods.js
File metadata and controls
86 lines (78 loc) · 2.4 KB
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
function getCookie(name){
var cookie = ' ' + document.cookie;
var search = ' ' + name + '=';
var setStr = null;
var offset = 0;
var end = 0;
if (cookie.length > 0){
offset = cookie.indexOf(search);
if (offset !== -1){
offset += search.length;
end = cookie.indexOf(';',offset);
if (end === -1)
end = cookie.length;
setStr = unescape(cookie.substring(offset,end));
}
}
if ((setStr != null) && (setStr.charAt(0) === "\"") && (setStr.charAt(setStr.length - 1) === "\"")){
setStr = setStr.substr(1, setStr.length - 2);
}
return (setStr);
};
function setCookie(name, value, options) {
options = options || {};
var expires = options.expires;
if (typeof expires === "number" && expires) {
var d = new Date();
d.setTime(d.getTime() + expires*1000);
expires = options.expires = d;
}
if (expires && expires.toUTCString) {
options.expires = expires.toUTCString();
}
value = encodeURIComponent(value);
var updatedCookie = name + "=" + value;
for(var propName in options) {
updatedCookie += "; " + propName;
var propValue = options[propName];
if (propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
};
function Ajax(method, adress, params, headers, respFunc){
if ((method !== "GET")
&& (method !== "POST")
&& (method !== "DELETE")){
method = "GET";
}
var xhr = new XMLHttpRequest();
var paramsString = "";
if ( {}.toString.call(params) === '[object Object]' ) {
var num = 0;
for(var key in params) {
if (num === 0){
paramsString = "?";
}else{
paramsString += "&";
};
paramsString += key + "=" + params[key];
num++;
};
};
xhr.open(method, adress + paramsString, true);
if (typeof respFunc === "function") {
xhr.onreadystatechange = function() {
if (this.readyState !== 4) return;
respFunc(xhr.responseText);
};
};
if ( {}.toString.call(headers) === '[object Object]' ) {
for(var key in headers) {
xhr.setRequestHeader(key, headers[key]);
};
};
xhr.send("");
return xhr;
};