当前位置:编程学习 > C#/ASP.NET >>

JS读取和添加cookie,

我想要写段JS脚本。就是添加COOKIE和读取的。我现在的需求是比如我有个要存入的数据我就想存这个p这个字母,然后我想要用JS在读出cookie里的这个p字母,
在问一个。比如我存进去字母p了。然后我想在存入一个字母Z,但是是覆盖P的。就是P没了。读取时候变成读出来的是Z。求驻怎么写。。。 --------------------编程问答-------------------- 下载这个文件,jquery.cookie.js,js读写cookie很简单! --------------------编程问答--------------------
$.cookie('the_cookie'); // 读取 cookie
$.cookie('the_cookie', 'the_value'); // 存储 cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); // 存储一个带7天期限的 cookie
$.cookie('the_cookie', '', { expires: -1 }); // 删除 cookie 
--------------------编程问答--------------------
引用 2 楼 guwei4037 的回复:
$.cookie('the_cookie'); // 读取 cookie
$.cookie('the_cookie', 'the_value'); // 存储 cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); // 存储一个带7天期限的 cookie
$.cookie('the_cookie', '', { expires: -1 }); // 删除 cookie 
麻烦您按照我的要求给我写出来被。你这么写我还是不会 --------------------编程问答-------------------- --------------------编程问答--------------------
<script type="text/javascript">
  $(function(){
    $.cookie("p_value","p");//cookie名称为p_value,值为p
    $.cookie("p_value");//读取p这个字母
    $.cookie("p_value","z");//cookie名称为p_value,值为z,此时就覆盖了p这个值
    $.cookie("p_value");//再次取值时,为z
  });
</script>

--------------------编程问答--------------------
引用 5 楼 guwei4037 的回复:
<script type="text/javascript">
  $(function(){
    $.cookie("p_value","p");//cookie名称为p_value,值为p
    $.cookie("p_value");//读取p这个字母
    $.cookie("p_value","z");//cookie名称为p_value,值为z,此时就覆盖了p这个值
    $.cookie("p_value");//再次取值时,为z
  });
</script>
JS脚本和JQ能混合写么 --------------------编程问答--------------------
引用 6 楼 u010522369 的回复:
Quote: 引用 5 楼 guwei4037 的回复:

<script type="text/javascript">
  $(function(){
    $.cookie("p_value","p");//cookie名称为p_value,值为p
    $.cookie("p_value");//读取p这个字母
    $.cookie("p_value","z");//cookie名称为p_value,值为z,此时就覆盖了p这个值
    $.cookie("p_value");//再次取值时,为z
  });
</script>
JS脚本和JQ能混合写么


当然可以,不影响。 --------------------编程问答--------------------
引用 7 楼 guwei4037 的回复:
Quote: 引用 6 楼 u010522369 的回复:

Quote: 引用 5 楼 guwei4037 的回复:

<script type="text/javascript">
  $(function(){
    $.cookie("p_value","p");//cookie名称为p_value,值为p
    $.cookie("p_value");//读取p这个字母
    $.cookie("p_value","z");//cookie名称为p_value,值为z,此时就覆盖了p这个值
    $.cookie("p_value");//再次取值时,为z
  });
</script>
JS脚本和JQ能混合写么


当然可以,不影响。
还得麻烦下。那个cookie怎么判断2次的cookie值不想通呀。删除cookie怎么写呀。 --------------------编程问答--------------------
引用 8 楼 u010522369 的回复:
Quote: 引用 7 楼 guwei4037 的回复:

Quote: 引用 6 楼 u010522369 的回复:

Quote: 引用 5 楼 guwei4037 的回复:

<script type="text/javascript">
  $(function(){
    $.cookie("p_value","p");//cookie名称为p_value,值为p
    $.cookie("p_value");//读取p这个字母
    $.cookie("p_value","z");//cookie名称为p_value,值为z,此时就覆盖了p这个值
    $.cookie("p_value");//再次取值时,为z
  });
</script>
JS脚本和JQ能混合写么


当然可以,不影响。
还得麻烦下。那个cookie怎么判断2次的cookie值不想通呀。删除cookie怎么写呀。


<script type="text/javascript">
  $(function(){
    if($.cookie("p_value")!='xxx')
    {
        //xxx是你第二次取到的值
        $.cookie('p_value', '', { expires: -1 }); // 删除 cookie 
    }
   
  });
</script> --------------------编程问答-------------------- 下载jquery.cookie.js
调用:
$(document).ready(function () {
    $('#wCookies').click(function () {
        $.cookie('name', 'test', { expires: 7 });
    });
    $('#rCookies').click(function () {
        var test = $.cookie('name');
        alert(test);
    });
    $('#dCookies').click(function () {
        $.cookie('name', null);
    });
}); --------------------编程问答--------------------
/*!
 * jQuery Cookie Plugin
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2011, Klaus Hartl
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/GPL-2.0
 */
 
/*①、Create expiring cookie, 7 days from then:*/
//$.cookie('the_cookie', 'the_value', { expires: 7 });
/*②、Read cookie:*/
//$.cookie('the_cookie'); // => 'the_value' or Null
/*③、Delete cookie by passing null as value:*/
//$.cookie('the_cookie', null); or $.cookie('the_cookie', '', { expires: -1 });

            
(function($) {
    $.cookie = function(key, value, options) {

        // key and at least value given, set cookie...
        if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
            options = $.extend({}, options);

            if (value === null || value === undefined) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            value = String(value);

            return (document.cookie = [
                encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        // key and possibly options given, get cookie...
        options = value || {};
        var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

        var pairs = document.cookie.split('; ');
        for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
            if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
        }
        return null;
    };
})(jQuery);
--------------------编程问答--------------------
引用 9 楼 guwei4037 的回复:
Quote: 引用 8 楼 u010522369 的回复:

Quote: 引用 7 楼 guwei4037 的回复:

Quote: 引用 6 楼 u010522369 的回复:

Quote: 引用 5 楼 guwei4037 的回复:

<script type="text/javascript">
  $(function(){
    $.cookie("p_value","p");//cookie名称为p_value,值为p
    $.cookie("p_value");//读取p这个字母
    $.cookie("p_value","z");//cookie名称为p_value,值为z,此时就覆盖了p这个值
    $.cookie("p_value");//再次取值时,为z
  });
</script>
JS脚本和JQ能混合写么


当然可以,不影响。
还得麻烦下。那个cookie怎么判断2次的cookie值不想通呀。删除cookie怎么写呀。


<script type="text/javascript">
  $(function(){
    if($.cookie("p_value")!='xxx')
    {
        //xxx是你第二次取到的值
        $.cookie('p_value', '', { expires: -1 }); // 删除 cookie 
    }
   
  });
</script>
如果我现在不知道我目前获取到的cookie的值的名字就是前面的健能有啥办法获取到这个名字么 --------------------编程问答--------------------
引用 10 楼 fangxuan 的回复:
下载jquery.cookie.js
调用:
$(document).ready(function () {
    $('#wCookies').click(function () {
        $.cookie('name', 'test', { expires: 7 });
    });
    $('#rCookies').click(function () {
        var test = $.cookie('name');
        alert(test);
    });
    $('#dCookies').click(function () {
        $.cookie('name', null);
    });
});
不知道cookie的键名,怎么能知道呀 --------------------编程问答--------------------
引用 11 楼 Joyhen 的回复:
/*!
 * jQuery Cookie Plugin
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2011, Klaus Hartl
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/GPL-2.0
 */
 
/*①、Create expiring cookie, 7 days from then:*/
//$.cookie('the_cookie', 'the_value', { expires: 7 });
/*②、Read cookie:*/
//$.cookie('the_cookie'); // => 'the_value' or Null
/*③、Delete cookie by passing null as value:*/
//$.cookie('the_cookie', null); or $.cookie('the_cookie', '', { expires: -1 });

            
(function($) {
    $.cookie = function(key, value, options) {

        // key and at least value given, set cookie...
        if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
            options = $.extend({}, options);

            if (value === null || value === undefined) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            value = String(value);

            return (document.cookie = [
                encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        // key and possibly options given, get cookie...
        options = value || {};
        var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

        var pairs = document.cookie.split('; ');
        for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
            if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
        }
        return null;
    };
})(jQuery);
不知道cookie的键名,怎么能知道呀 --------------------编程问答-------------------- document.cookie="p";
alert(document.cookie);
详细出处参考:http://www.jb51.net/article/14566.htm
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,