• 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

  • 相关阅读:
    iOS项目的目录结构和开发流程
    XCode SVN设置
    iOS 登录 注册
    ios开发常用技巧
    iOS问题解答
    iOS设计模式
    iOS开发:打包应用程序
    iOS 封装
    iOS开发常用宏
    Objective-C 类,实例成员,静态变量,对象方法,类方法(静态方法),对象
  • 原文地址:https://www.cnblogs.com/xuelu/p/4127122.html
Copyright © 2020-2023  润新知