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("删除成功"); }