zoukankan      html  css  js  c++  java
  • COOKIE

    cookie 的 介绍 和封装 

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     5 <title>无标题文档</title>
     6 <script>
     7 /*
     8 cookie : 存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来像访问者电脑上存储数据
     9     1.不同的浏览器存放的cookie位置不一样,也是不能通用的
    10     2.cookie的存储是以域名形式进行区分的
    11     3.cookie的数据可以设置名字的
    12     4.一个域名下存放的cookie的个数是有限制的,不同的浏览器存放的个数不一样
    13     5.每个cookie存放的内容大小也是有限制的,不同的浏览器存放大小不一样
    14     
    15     我们通过document.cookie来获取当前网站下的cookie的时候,得到的字符串形式的值,他包含了当前网站下所有的cookie。他会把所有的cookie通过一个分号+空格的形式串联起来
    16     
    17     如果我们想长时间存放一个cookie。需要在设置这个cookie的时候同时给他设置一个过期的时间
    18     cookie默认是临时存储的,当浏览器关闭进程的时候自动销毁
    19 */
    20 
    21 //document.cookie = '名字=值';
    22 
    23 /*document.cookie = 'username=leo';
    24 document.cookie = 'age=32';*/
    25 
    26 //document.cookie = '名称=值;expires=' + 字符串格式的时间;
    27 
    28 var oDate = new Date();
    29 
    30 oDate.setDate( oDate.getDate() + 5 );
    31 
    32 //alert(typeof oDate)
    33 //alert(typeof oDate.toGMTString());
    34 
    35 //内容最好编码存放,encodeURI
    36 
    37 //alert( encodeURI('你好') );
    38 //alert( decodeURI('%E4%BD%A0%E5%A5%BD') )
    39 
    40 /*document.cookie = 'username='+ encodeURI('leo
    你好') +';expires=' + oDate.toGMTString();
    41 document.cookie = 'age=32';*/
    42 
    43 //document.cookie
    44 //alert(decodeURI(document.cookie));    //username=leo; age=32
    45 
    46 /*document.cookie = 'username=leo;expires=' + oDate.toGMTString();
    47 document.cookie = 'age=32';*/
    48 
    49 function setCookie(key, value, t) {
    50     var oDate = new Date();
    51     oDate.setDate( oDate.getDate() + t );
    52     document.cookie = key + '=' + value + ';expires=' + oDate.toGMTString();
    53 }
    54 
    55 function getCookie(key) {
    56     var arr1 = document.cookie.split('; ');
    57     for (var i=0; i<arr1.length; i++) {
    58         var arr2 = arr1[i].split('=');
    59         if ( arr2[0] == key ) {
    60             return decodeURI(arr2[1]);
    61         }
    62     }
    63 }
    64 
    65 function removeCookie(key) {
    66     setCookie(key, '', -1);
    67 }
    68 
    69 
    70 //setCookie('sex','男', 10);
    71 //alert( getCookie('age') );
    72 removeCookie('username');
    73 removeCookie('age');
    74 removeCookie('sex');
    75 
    76 alert(document.cookie)
    77 </script>
    78 </head>
    79 
    80 <body>
    81 </body>
    82 </html>

    cookie 的应用

     1 <!DOCTYPE HTML>
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     5 <title>无标题文档</title>
     6 <script>
     7 window.onload = function() {
     8     
     9     var oUsername = document.getElementById('username');
    10     var oLogin = document.getElementById('login');
    11     var oDel = document.getElementById('del');
    12     
    13     if ( getCookie('username') ) {
    14         oUsername.value = getCookie('username');
    15     }
    16     
    17     oLogin.onclick = function() {
    18         
    19         alert('登陆成功');
    20         setCookie('username', oUsername.value, 5);
    21         
    22     }
    23     
    24     oDel.onclick = function() {
    25         removeCookie('username');
    26         oUsername.value = '';
    27     }
    28     
    29 }
    30 
    31 function setCookie(key, value, t) {
    32     var oDate = new Date();
    33     oDate.setDate( oDate.getDate() + t );
    34     document.cookie = key + '=' + value + ';expires=' + oDate.toGMTString();
    35 }
    36 
    37 function getCookie(key) {
    38     var arr1 = document.cookie.split('; ');
    39     for (var i=0; i<arr1.length; i++) {
    40         var arr2 = arr1[i].split('=');
    41         if ( arr2[0] == key ) {
    42             return decodeURI(arr2[1]);
    43         }
    44     }
    45 }
    46 
    47 function removeCookie(key) {
    48     setCookie(key, '', -1);
    49 }
    50 </script>
    51 </head>
    52 
    53 <body>
    54     <input type="text" id="username" />
    55     <input type="button" value="登陆" id="login" />
    56     <input type="button" value="删除" id="del" />
    57 </body>
    58 </html>
  • 相关阅读:
    XML错误信息Referenced file contains errors (http://www.springframework.org/schema/beans/spring-beans-4.0.xsd). For more information, right click on the message in the Problems View ...
    Description Resource Path Location Type Cannot change version of project facet Dynamic Web Module to 2.3.
    maven创建web报错Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins:maven-compiler-plugin:maven-compiler-plugin:3.5.1:runtime Cause: error in opening zip file
    AJAX跨域
    JavaWeb学习总结(转载)
    JDBC学习笔记
    Java动态代理之JDK实现和CGlib实现
    (转)看懂UML类图
    spring boot配置使用fastjson
    python3下django连接mysql数据库
  • 原文地址:https://www.cnblogs.com/wanqiu/p/4459908.html
Copyright © 2011-2022 走看看