前段时间学到了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=0 ; i<arr.length;i++){ var arr2 = arr[i].split('='); if(arr2[0] == name){ return arr2[1] } } return '' } //删除 function removeCookie(name){ addCookie(name,'',-1) } window.onload = function(){ var oForm = document.getElementsByTagName("form")[0]; var oUser = document.getElementsByName("user")[0]; var oPass = document.getElementsByName("pwd")[0]; 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,7); 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>