前段时间学到了cookie,之前的公司用的jquery插件,现在终于学到了原生的js
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="智能社 - zhinengshe.com" />
<meta name="copyright" content="智能社 - zhinengshe.com" />
<title>智能社 - www.zhinengshe.com</title>
<script>
//添加
function addCookie(name,value,iDay){
if(iDay){
var oDate = new Date();
oDate.setDate(oDate.getDate() + iDay);
document.cookie = name +'='+value+'; path=/;expires='+oDate;
}else{
document.cookie = name +'='+value+'; path=/'
}
}
//查找
function getCookie(name){
var arr = document.cookie.split(';');
for(var i= ; i<arr.length;i++){
var arr2 = arr[i].split('=');
if(arr2[] == name){
return arr2[]
}
}
return ''
} //删除
function removeCookie(name){
addCookie(name,'',-)
} window.onload = function(){
var oForm = document.getElementsByTagName("form")[];
var oUser = document.getElementsByName("user")[];
var oPass = document.getElementsByName("pwd")[]; var user = getCookie("user");
var pwd = getCookie("pwd"); oUser.value = user;
oPass.value = pwd; oForm.onsubmit = function(){ if(oUser.value=="" || oPass.value== ""){ return false;
} addCookie("user",oUser.value,);
addCookie("pwd",oPass.value);
//removeCookie("user")
}; }; </script>
</head> <body>
<form action="http://www.zhinengshe.com/">
用户名:<input name="user" type="text" value="" /><br/>
密 码:<input name="pwd" type="text" value="" /><br/>
<input type="submit" />
</form>
</body>
</html>