cookie设置

问题: cookie设置好后,谷歌浏览器下-->只有本页面有值,但是在其它浏览器下正常.

$.cookie("userName",$("#loginName").val(),{expires:7});
$.cookie("password",$("#loginPW").val(),{expires:7});    

原因是: 木有设置cookie路径...--> 搞定

$.cookie("userName",$("#loginName").val(),{expires:7,path:‘/‘});
$.cookie("password",$("#loginPW").val(),{expires:7,path:‘/‘});    

使用了jquery.cookie.js: 代码如下-->

/*jslint browser: true */ /*global jQuery: true */

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
//Downloads By http://www.veryhuo.com
// TODO JsDoc

/**
 * Create a cookie with the given key and value and other optional parameters.
 *
 * @example $.cookie(‘the_cookie‘, ‘the_value‘);
 * @desc Set the value of a cookie.
 * @example $.cookie(‘the_cookie‘, ‘the_value‘, { expires: 7, path: ‘/‘, domain: ‘jquery.com‘, secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie(‘the_cookie‘, ‘the_value‘);
 * @desc Create a session cookie.
 * @example $.cookie(‘the_cookie‘, null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String key The key of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/[email protected]
 */

/**
 * Get the value of a cookie with the given key.
 *
 * @example $.cookie(‘the_cookie‘);
 * @desc Get the value of a cookie.
 *
 * @param String key The key of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/[email protected]
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.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 result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp(‘(?:^|; )‘ + encodeURIComponent(key) + ‘=([^;]*)‘).exec(document.cookie)) ? decode(result[1]) : null;
};
时间: 2024-10-20 02:47:37

cookie设置的相关文章

cookie 设置 httpOnly属性

cookie 设置 httpOnly属性防止js读取cookie. 建立filter拦截器类 CookieHttpOnlyFilter import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.serv

用Okhttp框架登录之后的Cookie设置到webView中(转)

用Okhttp框架登录之后的Cookie设置到webView中(正文) 1.webview中设置: [java] view plain copy @SuppressWarnings("deprecation") public void synCookies(Context context, String url) { CookieSyncManager.createInstance(context); CookieManager cookieManager = CookieManage

cookie设置httponly属性防护XSS攻击

攻击者利用XSS漏洞获取cookie或者session劫持,如果这里面包含了大量敏感信息(身份信息,管理员信息)等,攻击这里用获取的COOKIE登陆账号,并进行非法操作. COOKIE设置httponly属性可以化解XSS漏洞攻击带来的窃取cookie的危害. PHP中COOKIE设置方法: <?php setcookie("xsstest", "xsstest", time()+3600, "/", "", false

自己编写的一个Cookie设置与获取函数

自己编写的一个Cookie设置与获取函数,大家有什么感觉需要改进的地方,请告知与我,我一定虚心接受. Code: 1 function setCookie(name,value,time){ 2 if(name){ 3 var date = new Date(); 4 if(time){ 5 var lastword = time.slice(-1); 6 switch(lastword){ 7 case 'd' : date.setDate(date.getDate()+parseInt(ti

浏览器因cookie设置HttpOnly标志引起的安全问题

1.简介 如果cookie设置了HttpOnly标志,可以在发生XSS时避免JavaScript读取cookie,这也是HttpOnly被引入的 原因.但这种方式能防住攻击者吗?HttpOnly标志可以防止cookie被"读取",那么能不能防止被"写"呢?答案是否定的,那么这里面就有文章可 做了,因为已证明有些浏览器的HttpOnly标记可以被JavaScript写入覆盖,而这种覆盖可能被攻击者利用发动session fixation攻击.本文主题就是讨论这种技术.

★★★【卡法 常用js库】: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度

[卡法 常用js库]: js汇合 表单验证  cookie设置  日期格式  电话手机号码  email  整数  小数  金额   检查参数长度 // +---------------------------------------------------------------------- // | sunqiang // +---------------------------------------------------------------------- // | Copyrig

go 代理服务器(cookie设置失败无法登录)

1 Method POST 2 URL https://vjudge.net/user/login 3 Proto HTTP/1.1 4 ProtoMajor 1 5 ProtoMinor 1 6 Header map[Referer:[http://127.0.0.1:8002/] Accept-Language:[zh-CN,zh;q=0.8] Connection:[keep-alive] Origin:[http://127.0.0.1:8002] Content-Type:[appli

JS cookie 设置 查看 删除

JScookie 常用的3个预设函数(库) <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> function setCookie(name, value, iDay) //iday是多少天后过期 { var oDate=new Date(); oDate.setDate(oDate.ge

safari cookie设置中文失败

最近用H5进行手机端开发,由于是window操作系统,为了方便开发和调试,直接在chrome浏览器上进行测试,然后在android机上进行手机端测试,当功能基本完工后,原来在android上运行正常的应用,在IOS上运行时,出现很多奇怪的问题,根据排查,发现是由于cookie未取到值而导致相关信息无法获取. 一开始以为是cookie中文乱码的问题,后来跟踪发现,cookie的值压根就没赋值成功,网上查了资料,发现safari不允许非ASCII编码的值,换句话说:不允许中文存储. 为了解决这个问题