zoukankan      html  css  js  c++  java
  • jquery设置cookie

    1、下载jquery-1.11.1.js,因为jquery2不支持IE8,所以使用了jquery1版本

    2、下载jquery的cookie插件

    jQuery.cookie = function(name, value, options) {   
        if (typeof value != 'undefined') { // name and value given, set cookie  
            options = options || {};  
            if (value === null) {  
                value = '';  
                options.expires = -1;  
            }  
            var expires = '';  
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {  
                var date;  
                if (typeof options.expires == 'number') {  
                    date = new Date();  
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));  
                } else {  
                    date = options.expires;  
                }  
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE  
            }  
            // CAUTION: Needed to parenthesize options.path and options.domain  
            // in the following expressions, otherwise they evaluate to undefined  
            // in the packed version for some reason...  
            var path = options.path ? '; path=' + (options.path) : '';  
            var domain = options.domain ? '; domain=' + (options.domain) : '';  
            var secure = options.secure ? '; secure' : '';    
            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');  
        } else { // only name given, get cookie  
            var cookieValue = null;  
            if (document.cookie && document.cookie != '') {  
                var cookies = document.cookie.split(';');  
                for (var i = 0; i < cookies.length; i++) {  
                    var cookie = jQuery.trim(cookies[i]);  
                    // Does this cookie string begin with the name we want?  
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {  
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  
                        break;  
                    }  
                }  
            }  
            return cookieValue;  
        }  
    };  

    3、编写测试页面

    <html>
    <head>
    <script src="../jquery-1.11.1.js"></script>
    <script src="jquery-cookie.js"></script>
    <script>
    $(document).ready(function(){
     $("#btn1").click(function(){
      $.cookie('the_cookie', 'the_value',{expires:2}); 
    });
    $("#btn2").click(function(){
      var cook = $.cookie('the_cookie'); //读取Cookie值
      alert(cook);
    });
    });
    </script>
    </head>
    <body>
    <input type="button" id="btn1" value="1" />
    <input type="button" id="btn2" value="2" />
    
    <input type="text" id="test" value="test"></input>
    <body>
    
    
    </html>

    4、将代码发布到测试服务器上,通过python来访问,启用方式如下:

     http://www.cnblogs.com/xuelu/p/4127112.html

    5、在本地访问测试页面

    http://100.15.6.160:8000/cookie/cookieTest.html

  • 相关阅读:
    Pycharm快捷键
    unittest自动化测试框架
    Python简介
    Git工作流介绍
    GitFlow ⼯作流
    go 整分钟开始执行程序
    vue 保留两位小数
    vue 格式化时间戳
    Supervisor-进程守护工具
    为什么计算机语言中的变量名都不能以数字开头呢?
  • 原文地址:https://www.cnblogs.com/xuelu/p/4127122.html
Copyright © 2011-2022 走看看