Javascript 如何设置两个jquery cookie?

Javascript 如何设置两个jquery cookie?,javascript,jquery,cookies,Javascript,Jquery,Cookies,我正在开发JavaEE应用程序,我有一个页面为不同的用户使用相同的cookie,这是不对的。因此,我复制了jquery.cookie.js文件并创建了一个名为jquery.cookie2.js的新文件,我还更改了调用脚本,它工作了,但具有相同的操作。无论我在老用户身上做什么,都会在新用户身上发生。我想可能是饼干的名字,所以我把它改成 jQuery.cookie 到 从那以后就没用了。 这是cookie的代码 jQuery.cookie = function(name, value, optio

我正在开发JavaEE应用程序,我有一个页面为不同的用户使用相同的cookie,这是不对的。因此,我复制了jquery.cookie.js文件并创建了一个名为jquery.cookie2.js的新文件,我还更改了调用脚本,它工作了,但具有相同的操作。无论我在老用户身上做什么,都会在新用户身上发生。我想可能是饼干的名字,所以我把它改成

jQuery.cookie

从那以后就没用了。 这是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;
}
};
jQuery.cookie=函数(名称、值、选项){
如果(typeof value!=“undefined”){//给定名称和值,则设置cookie
选项=选项| |{};
如果(值===null){
值=“”;
options.expires=-1;
}
var失效=“”;
if(options.expires&(typeof options.expires=='number'| | options.expires.toutString)){
风险值日期;
if(typeof options.expires==“number”){
日期=新日期();
date.setTime(date.getTime()+(options.expires*24*60*60*1000));
}否则{
日期=options.expires;
}
expires=';expires='+date.toutString();//使用expires属性,IE不支持最大年龄
}
//注意:需要将options.path和options.domain括起来
//在下列表达式中,否则它们的计算结果为未定义
//由于某种原因,在打包版本中。。。
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(“”);
}否则{//仅给出名称,获取cookie
var-cookieValue=null;
if(document.cookie&&document.cookie!=''){
var cookies=document.cookie.split(“;”);
对于(变量i=0;i

我的问题是如何为一个应用程序使用不同的cookie?

您刚刚复制并粘贴了cookie模块的源代码,这对您没有帮助。删除jquery.cookie2.js。如果要使用两个不同的Cookie,请设置两个不同Cookie的值:

//Set values
$.cookie('user1', 'firstValue');
$.cookie('user2', 'differentValue');

//Retrieve values
$.cookie('user1'); //Returns 'firstValue'
$.cookie('user2'); //Returns 'differentValue'
但我不确定您是否需要使用cookies,特别是如果您的目标是在同一台机器/浏览器上为不同的用户在客户端加载一些信息。有关使用
localStorage
的替代方法的详细信息,请参阅此答案,特别是在不需要在每次页面加载时将信息发送到服务器的情况下:

//Set values
$.cookie('user1', 'firstValue');
$.cookie('user2', 'differentValue');

//Retrieve values
$.cookie('user1'); //Returns 'firstValue'
$.cookie('user2'); //Returns 'differentValue'