js实现登录时记住用户名

在页面中如何获取cookie值呢?

如果是JSP的话,可以通过servlet的对象request 获取cookie,可以

参考:http://hw1287789687.iteye.com/blog/2050040

如果要求登录页面是html呢?html页面中如何获取cookie呢?

直接上代码了

页面:loginInput.html

代码:

Html代码   收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html><head>  
  3. <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  4. <!-- base href="http://localhost:8080/shop_goods/" -->  
  5.   
  6. <title>user login</title>  
  7.   
  8. <meta http-equiv="pragma" content="no-cache">  
  9. <meta http-equiv="cache-control" content="no-cache">  
  10. <meta http-equiv="expires" content="0">  
  11. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  12. <meta http-equiv="description" content="This is my page">  
  13. <script language="JavaScript" src="/shop_goods/js/common_util.js" type="text/javascript"></script>  
  14.               
  15. <!-- 
  16.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  17.     -->  
  18. <style type="text/css">  
  19. .errorMessage li {  
  20.     list-style-type: none;  
  21.     margin-left: 0  
  22. }  
  23. </style>  
  24. </head>  
  25.   
  26. <body>  
  27.   
  28. <script type="text/javascript">  
  29. //获取cookie的值  
  30. function getCookie(cookieKey){  
  31.     var cookies = document.cookie ? document.cookie.split('; ') : [];  
  32.   
  33.     for (var i = 0l = cookies.length; i < l; i++) {  
  34.         var parts = cookies[i].split('=');  
  35.         if(parts.length>1){  
  36.             if(parts[0]==cookieKey){  
  37.                 //username1=;  
  38.                 return parts[1];  
  39.             }  
  40.         }  
  41.     }  
  42.     return '';  
  43. }  
  44. var username1='';  
  45. window.onload=function(){  
  46.     //cookie的key是'userEmail'  
  47.     username1=getCookie('userEmail');  
  48.   
  49.     //alert("username1:"+username1);  
  50.     var issave222=com.whuang.hsj.$$one("issave");  
  51.     if(username1){  
  52.         if(username1!='' && username1!=null &&username1!=undefined){  
  53.             com.whuang.hsj.$$one("user.username").value=username1;  
  54.             issave222.checked=true;  
  55.         }else{  
  56.             issave222.checked=false;  
  57.         }  
  58.     }else{  
  59.         issave222.checked=false;  
  60.     }  
  61. }  
  62. </script>  
  63.     This is login page.  
  64.     <br>  
  65.     <a href="/shop_goods/">index</a>  
  66.     <br>  
  67.       
  68.         <a href="/shop_goods/user/registerUser.jsp">register user</a>  
  69.       
  70.     <font color="red"></font>  
  71.       
  72.     <font style="font-weight: bold" color="red"> </font>  
  73.               
  74.       
  75.       
  76.         <form action="/shop_goods/user/login" method="post">  
  77.             <table>  
  78.                 <tbody><tr>  
  79.                     <td>username:</td>  
  80.                     <td><input name="user.username" id="user_username" type="text">  
  81.                     </td>  
  82.                 </tr>  
  83.                 <tr>  
  84.                     <td>password:</td>  
  85.                     <td><input name="user.password" id="user_password" type="text">  
  86.                     </td>  
  87.                 </tr>  
  88.                 <tr> <td colspan="2"> <input name="issave" value="save" type="checkbox"> 保存用户名</td></tr>  
  89.                 <tr>  
  90.                     <td colspan="2"><input id="" value="login" type="submit">  
  91. </td>  
  92.                       
  93.                 </tr>  
  94.   
  95.             </tbody></table>  
  96.               
  97.   
  98.   
  99. </form></body></html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值