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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。