HTML5本地储存追加利用html5的本地存储功能实现登录用户信息保存

在HTML5中,本地存储是一个window的属性,包括localStorage和sessionStorage,从名字应该可以很清楚的辨认二者的区别,前者是一直存在本地的,后者只是伴随着session,窗口一旦关闭就没了。二者用法完全相同,这里以localStorage为例。

用户名、密码保存,自动登录等,可以通过设置cookie实现,第一次登录网站后在本地计算机的中写入cookie,之后再次登录此网站查看cookie中现有的值,用cookie值进行网站登录即可。但是 cookie 不适合大量数据的存储,因为它们由每个对服务器的请求来传递,这使得 cookie 速度很慢而且效率也不高。

HTML5提供了一个此类问题比较方便的解决方案,就是localstorage。数据不是由每个服务器请求传递的,而是只有在请求时使用数据。它使在不影响网站性能的情况下存储大量数据成为可能。对于不同的网站,数据存储于不同的区域,并且一个网站只能访问其自身的数据。html5 使用 JavaScript 来存储和访问数据。

下面是一个登录保存用户名密码的实例:

HTML5本地储存追加利用html5的本地存储功能实现登录用户信息保存_第1张图片

[html]  view plain  copy
  1. >  
  2.   
  3.   
  4.   
  5.   
  6.   
  7. <html lang="en" class="no-js">  
  8.   
  9. <head>  
  10. <meta charset="UTF-8" />  
  11.   
  12. <title>图书销售管理系统title>  
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  14. <meta name="description"  
  15.     content="Login and Registration Form with HTML5 and CSS3" />  
  16. <meta name="keywords"  
  17.     content="html5, css3, form, switch, animation, :target, pseudo-class" />  
  18. <meta name="author" content="Codrops" />  
  19. <link rel="shortcut icon" href="../favicon.ico">  
  20.   
  21. <script type="text/javascript" src="js/jquery.min.js">script>  
  22. head>  
  23. <script>  
  24.     //先查看是否有本地数据(记住密码)  
  25.     /* function myfunction(){  
  26.         var email = localStorage.getItem("email");  
  27.         var password = localStorage.getItem("password");  
  28.         if(email!=null&&password!=null){  
  29.             alert("邮箱:"+email+"密码:"+password);  
  30.             $.ajax({  
  31.                 url : '../servlet/LoginServlet?choose=login',  
  32.                 data : {  
  33.                     email : email,  
  34.                     password : password  
  35.                 },  
  36.                 dataType : 'json',  
  37.                 success : function(data) {  
  38.                     if (data.msg == "") {  
  39.                         alert("用户名或密码错误");  
  40.                     } else {  
  41.                     //登录成功后保存session,如果选择了记住密码,再保存到本地  
  42.                         //window.location.href ='../index/index.jsp';  
  43.                     }  
  44.                 },  
  45.                 error : function() {  
  46.                     alert("系统错误");  
  47.                 }  
  48.             });  
  49.         }else{  
  50.             //alert("没有信息");  
  51.               
  52.         }  
  53.     }  
  54.     function saveStorage(){  
  55.         var email = document.getElementById("email").value;  
  56.         localStorage.setItem("email",email);  
  57.         var password = document.getElementById("password").value;  
  58.         localStorage.setItem("password",password);  
  59.       
  60.     }  
  61.     function login(){  
  62.         $.ajax({  
  63.             url : '../servlet/LoginServlet?choose=login',  
  64.             data : {  
  65.                 email : $('#loginform input[name=email]').val(),  
  66.                 password : $('#loginform input[name=password]').val()  
  67.             },  
  68.             dataType : 'json',  
  69.             success : function(data) {  
  70.                 if (data.msg == "") {  
  71.                     alert("用户名或密码错误");  
  72.                 } else {  
  73.                 //登录成功后保存session,如果选择了记住密码,再保存到本地  
  74.                     window.location.href ='../index/index.jsp';  
  75.                 }  
  76.             },  
  77.             error : function() {  
  78.                 alert("系统错误");  
  79.             }  
  80.        });  
  81.     }  
  82.     function reg(){  
  83.         $.ajax({  
  84.             url : 'servlet/LoginServlet?choose=reg',  
  85.             data : {  
  86.                 username : $('#regform input[name=username]').val(),  
  87.                 password : $('#regform input[name=password]').val(),  
  88.                 email : $('#regform input[name=email]').val(),  
  89.             },    
  90.             dataType : 'json',  
  91.             success : function(data) {  
  92.                 if(data.msg == false){  
  93.                     alert("注册失败");  
  94.                 }else{  
  95.                     alert("注册成功请返回登录");  
  96.                 }  
  97.             },  
  98.             error : function() {  
  99.                 alert("系统错误");  
  100.             }  
  101.         });  
  102.     } */  
  103. $(document).ready(function(){    
  104.   //读取 localStage 本地存储,填充用户名密码,如果自动登录有值直接跳转;    
  105.   //相反,跳转到本页面,等待登陆处理    
  106.   var storage = window.localStorage;    
  107.     var getEmail = storage["email"];    
  108.     var getPwd = storage["password"];    
  109.     var getisstroepwd = storage["isstorePwd"];    
  110.     var getisautologin = storage["isautologin"];    
  111.     if("yes" == getisstroepwd)    
  112.     {    
  113.         if("yes" == getisautologin)    
  114.         {    
  115.             if(( ("" != getEmail) ||(null != getEmail)) && (("" != getPwd) ||(null != getPwd)))    
  116.             {    
  117.                 //lacoste  已经保存 登陆信息 直接登陆    
  118.                 // alert('正在自动登录');  
  119.                 $("email").val(getEmail);  
  120.                 $("#password").val(getPwd);    
  121.                // window.location="";     
  122.                //加载时显示:正在自动登录  
  123.                 $.ajax({  
  124.                     url : '../servlet/LoginServlet?choose=login',  
  125.                     data : {  
  126.                         email :  getEmail,  
  127.                         password : getPwd   
  128.                     },  
  129.                     dataType : 'json',  
  130.                     success : function(data) {  
  131.                         if (data.msg == "") {  
  132.                             alert("账号信息异常,请核实后重新登录");  
  133.                         } else {  
  134.                         //登录成功后保存session,如果选择了记住密码,再保存到本地  
  135.                             //window.location.href ='../index/index.jsp';  
  136.                         }  
  137.                     },  
  138.                     error : function() {  
  139.                         alert("系统错误");  
  140.                     }  
  141.                 });  
  142.             }    
  143.         }    
  144.         else    
  145.         {       
  146.                 $("#email").val(getEmail);    
  147.                 $("#password").val(getPwd);    
  148.                 document.getElementById("isRemberPwdId").checked = true;    
  149.         }    
  150.     }    
  151.  });    
  152. function login(){  
  153.      var userEmail=$("#email").val();    
  154.     var userPassWord=$("#password").val();    
  155.     
  156.     var storage = window.localStorage;    
  157.     //记住密码    
  158.     if(document.getElementById("isRemberPwdId").checked)    
  159.     {    
  160.         //存储到loaclStage      
  161.     
  162.         storage["email"] = userEmail;    
  163.         storage["password"] =  userPassWord;    
  164.         storage["isstorePwd"] =  "yes";    
  165.     }    
  166.     else    
  167.     {    
  168.         storage["email"] = userEmail;    
  169.         storage["isstorePwd"] =  "no";    
  170.     }    
  171.     
  172.     //下次自动登录    
  173.     if(document.getElementById("isAutoLoginId").checked)    
  174.     {    
  175.         //存储到loaclStage      
  176.         storage["email"] = userEmail;    
  177.         storage["password"] =  userPassWord;    
  178.         storage["isstorePwd"] =  "yes";    
  179.         storage["isautologin"] =  "yes";    
  180.     }    
  181.     else    
  182.     {    
  183.         storage["email"] = userEmail;    
  184.         storage["isautologin"] =  "no";    
  185.     }   
  186.      $.ajax({  
  187.             url : '../servlet/LoginServlet?choose=login',  
  188.             data : {  
  189.                 email : userEmail,  
  190.                 password : userPassWord  
  191.             },  
  192.             dataType : 'json',  
  193.             success : function(data) {  
  194.                 if (data.msg == "") {  
  195.                     alert("用户名或密码错误");  
  196.                 } else {  
  197.                 //登录成功后保存session,如果选择了记住密码,再保存到本地  
  198.                     window.location.href ='../index/index.jsp';  
  199.                 }  
  200.             },  
  201.             error : function() {  
  202.                 alert("系统错误");  
  203.             }  
  204.        });   
  205.        alert("登录成功");  
  206. }  
  207. script>  
  208. <body>  
  209.     <div class="container">  
  210.         <header>  
  211.   
  212.         header>  
  213.         <section>  
  214.               
  215.             <div id="container_demo">  
  216.                 <a class="hiddenanchor" id="toregister">a> <a class="hiddenanchor"  
  217.                     id="tologin">a>  
  218.                 <div id="wrapper">  
  219.                     <div id="login" class="animate form">  
  220.                         <form id="loginform" name="loginform" autocomplete="on" method="post">  
  221.                             <h1>登录h1>  
  222.                             <p>  
  223.                                 <label for="username" class="uname" data-icon="u"> 邮箱  label>  
  224.                                 <input id="email" name="email" required="required"  
  225.                                     type="text" placeholder="请输入邮箱" autofocus />  
  226.                             p>  
  227.                             <p>  
  228.                                 <label for="password" class="youpasswd" data-icon="p">  
  229.                                     密码label> <input id="password" name="password" required="required"  
  230.                                     type="password" placeholder="请输入密码" />  
  231.                             p>  
  232.                             <p class="keeplogin">  
  233.                                 <input type="checkbox" name="isRemberPwdId" id="isRemberPwdId"  
  234.                                      /> <label for="loginkeeping">记住密码 label>  
  235.                             p>  
  236.                             <p class="keeplogin">  
  237.                                 <input type="checkbox" name="isAutoLoginId" id="isAutoLoginId"  
  238.                                      /> <label for="autologin">自动登录 label>  
  239.                             p>  
  240.                             <p class="login button">  
  241.                                 <input type="button" value="登录" onclick="login()" />  
  242.                             p>  
  243.                             <p class="change_link">  
  244.                                 没有账号? <a href="#toregister" class="to_register">前去注册a>  
  245.                             p>  
  246.                         form>  
  247.                     div>  
  248.                 div>  
  249.             div>  
  250.         section>  
  251.     div>  
  252. body>  

  1. html>  
  2. 
    
    
    
    
    
    <html lang="en" class="no-js">
    
    <head>
    <meta charset="UTF-8" />
    
    <title>图书销售管理系统title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description"
      content="Login and Registration Form with HTML5 and CSS3" />
    <meta name="keywords"
      content="html5, css3, form, switch, animation, :target, pseudo-class" />
    <meta name="author" content="Codrops" />
    <link rel="shortcut icon" href="../favicon.ico">
    
    <script type="text/javascript" src="js/jquery.min.js">script>
    head>
    <script>
      //先查看是否有本地数据(记住密码)
      /* function myfunction(){
        var email = localStorage.getItem("email");
        var password = localStorage.getItem("password");
        if(email!=null&&password!=null){
          alert("邮箱:"+email+"密码:"+password);
          $.ajax({
            url : '../servlet/LoginServlet?choose=login',
            data : {
              email : email,
              password : password
            },
            dataType : 'json',
            success : function(data) {
              if (data.msg == "") {
                alert("用户名或密码错误");
              } else {
              //登录成功后保存session,如果选择了记住密码,再保存到本地
                //window.location.href ='../index/index.jsp';
              }
            },
            error : function() {
              alert("系统错误");
            }
          });
        }else{
          //alert("没有信息");
          
        }
      }
      function saveStorage(){
        var email = document.getElementById("email").value;
        localStorage.setItem("email",email);
        var password = document.getElementById("password").value;
        localStorage.setItem("password",password);
      
      }
      function login(){
        $.ajax({
          url : '../servlet/LoginServlet?choose=login',
          data : {
            email : $('#loginform input[name=email]').val(),
            password : $('#loginform input[name=password]').val()
          },
          dataType : 'json',
          success : function(data) {
            if (data.msg == "") {
              alert("用户名或密码错误");
            } else {
            //登录成功后保存session,如果选择了记住密码,再保存到本地
              window.location.href ='../index/index.jsp';
            }
          },
          error : function() {
            alert("系统错误");
          }
         });
        }
      function reg(){
        $.ajax({
          url : 'servlet/LoginServlet?choose=reg',
          data : {
            username : $('#regform input[name=username]').val(),
            password : $('#regform input[name=password]').val(),
            email : $('#regform input[name=email]').val(),
          },	
          dataType : 'json',
          success : function(data) {
            if(data.msg == false){
              alert("注册失败");
            }else{
              alert("注册成功请返回登录");
            }
          },
          error : function() {
            alert("系统错误");
          }
        });
      } */
    $(document).ready(function(){  
      //读取 localStage 本地存储,填充用户名密码,如果自动登录有值直接跳转;  
      //相反,跳转到本页面,等待登陆处理  
      var storage = window.localStorage;  
        var getEmail = storage["email"];  
        var getPwd = storage["password"];  
        var getisstroepwd = storage["isstorePwd"];  
        var getisautologin = storage["isautologin"];  
        if("yes" == getisstroepwd)  
        {  
            if("yes" == getisautologin)  
            {  
                if(( ("" != getEmail) ||(null != getEmail)) && (("" != getPwd) ||(null != getPwd)))  
                {  
                    //lacoste  已经保存 登陆信息 直接登陆  
                    // alert('正在自动登录');
                    $("email").val(getEmail);
                    $("#password").val(getPwd);  
                   // window.location="";   
                   //加载时显示:正在自动登录
                    $.ajax({
              url : '../servlet/LoginServlet?choose=login',
              data : {
                email :  getEmail,
                password : getPwd 
              },
              dataType : 'json',
              success : function(data) {
                if (data.msg == "") {
                  alert("账号信息异常,请核实后重新登录");
                } else {
                //登录成功后保存session,如果选择了记住密码,再保存到本地
                  //window.location.href ='../index/index.jsp';
                }
              },
              error : function() {
                alert("系统错误");
              }
         			});
                }  
            }  
            else  
            {     
                    $("#email").val(getEmail);  
                    $("#password").val(getPwd);  
                    document.getElementById("isRemberPwdId").checked = true;  
            }  
        }  
     });  
    function login(){
       var userEmail=$("#email").val();  
        var userPassWord=$("#password").val();  
      
        var storage = window.localStorage;  
        //记住密码  
        if(document.getElementById("isRemberPwdId").checked)  
        {  
            //存储到loaclStage    
      
            storage["email"] = userEmail;  
            storage["password"] =  userPassWord;  
            storage["isstorePwd"] =  "yes";  
        }  
        else  
        {  
            storage["email"] = userEmail;  
            storage["isstorePwd"] =  "no";  
        }  
      
        //下次自动登录  
        if(document.getElementById("isAutoLoginId").checked)  
        {  
            //存储到loaclStage    
            storage["email"] = userEmail;  
            storage["password"] =  userPassWord;  
            storage["isstorePwd"] =  "yes";  
            storage["isautologin"] =  "yes";  
        }  
        else  
        {  
            storage["email"] = userEmail;  
            storage["isautologin"] =  "no";  
        } 
         $.ajax({
          url : '../servlet/LoginServlet?choose=login',
          data : {
            email : userEmail,
            password : userPassWord
          },
          dataType : 'json',
          success : function(data) {
            if (data.msg == "") {
              alert("用户名或密码错误");
            } else {
            //登录成功后保存session,如果选择了记住密码,再保存到本地
              window.location.href ='../index/index.jsp';
            }
          },
          error : function() {
            alert("系统错误");
          }
         }); 
         alert("登录成功");
    }
    script>
    <body>
      <div class="container">
        <header>
    
        header>
        <section>
          
          <div id="container_demo">
            <a class="hiddenanchor" id="toregister">a> <a class="hiddenanchor"
              id="tologin">a>
            <div id="wrapper">
              <div id="login" class="animate form">
                <form id="loginform" name="loginform" autocomplete="on" method="post">
                  <h1>登录h1>
                  <p>
                    <label for="username" class="uname" data-icon="u"> 邮箱  label>
                    <input id="email" name="email" required="required"
                      type="text" placeholder="请输入邮箱" autofocus />
                  p>
                  <p>
                    <label for="password" class="youpasswd" data-icon="p">
                      密码label> <input id="password" name="password" required="required"
                      type="password" placeholder="请输入密码" />
                  p>
                  <p class="keeplogin">
                    <input type="checkbox" name="isRemberPwdId" id="isRemberPwdId"
                       /> <label for="loginkeeping">记住密码 label>
                  p>
                  <p class="keeplogin">
                    <input type="checkbox" name="isAutoLoginId" id="isAutoLoginId"
                       /> <label for="autologin">自动登录 label>
                  p>
                  <p class="login button">
                    <input type="button" value="登录" onclick="login()" />
                  p>
                  <p class="change_link">
                    没有账号? <a href="#toregister" class="to_register">前去注册a>
                  p>
                form>
              div>
            div>
          div>
        section>
      div>
    body>
    html>

你可能感兴趣的:(学习笔记)