利用html5的本機存放區功能實現登入使用者資訊儲存

來源:互聯網
上載者:User

標籤:html5本機存放區   html5登入使用者資訊儲存   html5中session儲存   

在HTML5中,本機存放區是一個window的屬性,包括localStorage和sessionStorage,從名字應該可以很清楚的辨認二者的區別,前者是一直存在本地的,後者只是伴隨著session,視窗一旦關閉就沒了。二者用法完全相同,這裡以localStorage為例。

使用者名稱、密碼儲存,自動登入等,可以通過設定cookie實現,第一次登入網站後在本機電腦的中寫入cookie,之後再次登入此網站查看cookie中現有的值,用cookie值進行網站登入即可。但是 cookie 不適合大量資料的儲存,因為它們由每個對伺服器的請求來傳遞,這使得 cookie 速度很慢而且效率也不高。

HTML5提供了一個此類問題比較方便的解決方案,就是localstorage。資料不是由每個伺服器請求傳遞的,而是只有在請求時使用資料。它使在不影響網站效能的情況下儲存大量資料成為可能。對於不同的網站,資料存放區於不同的地區,並且一個網站只能訪問其自身的資料。HTML5 使用 JavaScript 來儲存和訪問資料。

下面是一個登入儲存使用者名稱密碼的執行個體:


<!DOCTYPE html><!--[if lt IE 7 ]> <html lang="en" class="no-js ie6 lt8"> <![endif]--><!--[if IE 7 ]>    <html lang="en" class="no-js ie7 lt8"> <![endif]--><!--[if IE 8 ]>    <html lang="en" class="no-js ie8 lt8"> <![endif]--><!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]--><!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class="no-js"><!--<![endif]--><head><meta charset="UTF-8" /><!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">  --><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>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.