cookie学习

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>

//设置cookie,(名称,值,过期时间天为单位)
function setCookie(name,value,iDay){
var oDate=new Date();
//设置时间
oDate.setDate(oDate.getDate()+iDay);
document.cookie=name+'='+value+';expires='+oDate;
}

//读取cookie(名称)
function getCookie(name){
//切割成数组
var arr= document.cookie.split('; ');
for(var i=0;i<arr.length;i++){
//再次切割成数组
var arr2 =arr[i].split('=');
if(arr2[0]== name ){
return arr2[1];
}
}
return ;//没有找到为空
}
//删除cookie值
function removeCookie(name){
setCookie(name,1,-1);
}

//用户记住用户名
window.onload=function(){
var oForm=document.getElementById("form1");
var oUser=document.getElementsByName('user')[0];
oForm.onsubmit=function(){
//设置cookie
setCookie('user',oUser.value,14);
};
//读取cookie
oUser.value=getCookie('user');
}


</script>

</head>


<body>
<form id="form1" action="http://www.baidu.com">
用户名:<input type="text" name="user" /><br />
密码:<input type="password" name="psaa" /><br />
<input type="submit" value="登陆" />
</form>


</body>
</html>

原文地址:https://www.cnblogs.com/xurui01/p/4509848.html