cookie应用:记住上一次用户名和密码
Posted 花雨伞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cookie应用:记住上一次用户名和密码相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form class="form" action="http://www.baidu.com/"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="登录"/> </form> </body> <script type="text/javascript"> window.onload = function(){ var oForm = document.getElementsByTagName(‘form‘)[0]; var oUsername = document.getElementsByName(‘username‘)[0]; var oPassword = document.getElementsByName(‘password‘)[0]; oForm.onsubmit = function(){ setCookie(‘username‘, oUsername.value, 14); setCookie(‘password‘, oPassword.value, 14); }; oUsername.value = getCookie(‘username‘); oPassword.value = getCookie(‘password‘); }; //设置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 array1 = document.cookie.split("; "); for(var i = 0; i < array1.length; i++ ){ var array2 = array1[i].split(‘=‘); if(array2[0] == name){ return array2[1]; } }; return ‘‘; }; //删除cookie function removeCookie(name){ setCookie(name, null, -1); }; </script> </html>
以上是关于cookie应用:记住上一次用户名和密码的主要内容,如果未能解决你的问题,请参考以下文章