1
81041
2019-10-18 cc4059f9f5a2cd6766f4d888bd35d19f3827a053
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
function $(id) {
    return document.getElementById(id);
}
 
function geib(id) {
    return document.getElementById(id);
}
 
function elesByName(name) {
    return document.getElementsByName(name);
}
 
function elesByTag(name) {
    return document.getElementsByTagName(name);
}
 
function createEle(name) {
    return document.createElement(name);
}
 
// 封装trim函数
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};
 
// 封装ajax
// 参数:请求方式,请求url,发送的数据,回调函数
function $ajax(method, url, data, callback) {
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        xhr = new ActiveXObject('Micsoft.XMLHTTP');
    }
    xhr.open(method, url, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200)
            callback(xhr.responseText);
        if(xhr.readyState == 4 && xhr.status == 500){
            //console.info(xhr.responseText);
            //window.location.href="login.jsp";
        }
    };
    if (method.toLowerCase() == "post") {
        xhr.setRequestHeader('Content-Type',
                'application/x-www-form-urlencoded');
    }
    xhr.send(data);
}
 
// Cookie
function getCookie(c_name) { // 根据分隔符取每个变量的值
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf("^", c_start);
            if (c_end == -1)
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
 
// 设置cookie中的用户名以及密码
function setCookie(c_name, n_value, p_name, p_value, expiredays) // 设置cookie
{
    
    console.info("="+expiredays+"==");
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(n_value) + "^" + p_name + "="
            + escape(p_value)
            + ((expiredays == null) ? "" : "^;expires=" + exdate.toGMTString());
}
 
function checkCookie() // 检测cookie是否存在,如果存在则直接读取,否则创建新的cookie
{
    var username = getCookie("username");
    var password = getCookie("password");
    console.info(password);
    if (username != null && username != "" && password != null
            && password != "") {
        document.getElementById("user").value = username;
        document.getElementById("password").value = password;
    } else {
        // username = prompt('Please enter your name:', "");
        // password = prompt('Please enter your name:', "");
        if (username != null && username != "" && password != null
                && password != "") {
            setCookie('username', username, 'password', password, 60);
        }
    }
}
 
function cleanCookie(c_name, p_name) { // 使cookie过期
    document.cookie = c_name + "=" + ";" + p_name + "="
            + ";expires=Thu, 01-Jan-70 00:00:01 GMT";
    console.info("删除成功");
}