js中cookie、sessionStorage、localStorage

來源:互聯網
上載者:User

標籤:ons   onclick   local   zha   logs   result   setcookie   ret   tmp   

一、cookie

 1 <!DOCTYPE html> 2 <html> 3     <head> 4         <meta charset="utf-8"> 5         <title></title> 6     </head> 7     <body> 8         <input type="text" name="username" id="username" value=""> 9         <input type="password" name="password" id="password" value="">10         <button type="button" name="button" id="btn">刪除cookie</button>11         <script type="text/javascript">12             // 1、設定、擷取13             var d = new Date();14             d.setHours(d.getHours() + 1);//1小時後到期15             // path=/ 此時的/代表的是網站中所有的目錄都可以訪問這個cookie16             document.cookie=‘username=zhangsan;expires=‘+d.toUTCString()+";path=/";//設定cookie17             document.cookie;//擷取所有的cookie18             console.log(document.cookie)19 20             // 2.設定、擷取、刪除cookie21             // setCookie(‘password‘,‘123456‘,0.5*3600*1000)//30分鐘後失效22             // setCookie(‘username‘,‘zhangsan‘,0.5*3600*1000)//30分鐘後失效23             var btnObj = document.getElementById(‘btn‘)24             btnObj.onclick = function(){25                 delCookie(‘username‘);26                 delCookie(‘password‘);27             }28             function setCookie(name,value,expires){29                 //計算有效期間30                 var d = new Date();31                 //設定有效期間32                 d.setTime(d.getTime()+expires);33                 //設定cookie34                 document.cookie = name +‘=‘+value+‘;expires=‘+d.toUTCString();35             }36             function getCookie(name) {37                 //擷取cookie38                 var cookieStr = document.cookie;39                 //根據;拆分40                 var cookieArr = cookieStr.split(‘;‘);41                 //擷取對應的cookie值42                 name += ‘=‘;//擷取的cookie值都是後面有=43                 var result = ‘‘;44                 for(var i=0;i<cookieArr.length;i++) {45                     //沒一個cookie值得前面都有一個空格,將空格刪除,再去尋找對應的name值46                     var tmp = cookieArr[i].trim();47                     if(tmp.indexOf(name) == 0) {48                         //如果尋找成功,則直接返回49                         result = tmp.slice(name.length);50                         break;51                     }52                 }53                 //返回結果54                 return result;55             }56             function delCookie(name) {57                 var d = new Date();58                 d.setTime(d.getTime()-1000);59                 document.cookie = name+‘=;expires=‘+d.toUTCString();60             }61         </script>62     </body>63 </html>

 

js中cookie、sessionStorage、localStorage

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.