jQuery.cookie.js中cookie设置遇到的问题
2012-04-01 15:32
585 查看
jQuery.cookie.js文件内容相信看到这的朋友都有了:
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
}
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;
}
}
向cookie中设置一个值后,另一个页面取不到cookie的值:
http://test.com/a/index.html中$.cookie('cookiename', 'test_val', {expires: 7, path: '/', domain: 'test.com', secure: true});
在http://test.com/b/index.html中$.cookie('cookiename')返回为空,而http://test.com/a/test.html中确返回 test_val,弄半天,domain设置没错,path也正确,,,,翻看各种api和各种搜索,,,还是找不到答案,我去!!!!!!!!
折腾N久,,,一步步测试,,,将secure设置成false后,,,能读取到,真是发现新大陆一样。。。。我擦!!!!
rocord下来。。。。。。。。
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
}
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;
}
}
向cookie中设置一个值后,另一个页面取不到cookie的值:
http://test.com/a/index.html中$.cookie('cookiename', 'test_val', {expires: 7, path: '/', domain: 'test.com', secure: true});
在http://test.com/b/index.html中$.cookie('cookiename')返回为空,而http://test.com/a/test.html中确返回 test_val,弄半天,domain设置没错,path也正确,,,,翻看各种api和各种搜索,,,还是找不到答案,我去!!!!!!!!
折腾N久,,,一步步测试,,,将secure设置成false后,,,能读取到,真是发现新大陆一样。。。。我擦!!!!
rocord下来。。。。。。。。
相关文章推荐
- 使用Jquery.cookie.js遇到的一些问题
- js/jquery使用过程遇到的问题
- js设置,删除,添加cookie,同时解决在目录下,不能设置、删除和添加的问题
- jquery.form.js 遇到的奇怪问题
- 解决html-jquery/js引用外部图片时遇到看不了或出现403的问题
- jquery.form.js 遇到的奇怪问题
- 遇到问题-----JS中设置window.location.href跳转无效
- IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题
- 遇到问题-----JS中设置window.location.href跳转无效
- JS/Jquery使用过程中遇到的问题和解决方法
- jQuery全屏滚动插件fullPage.js下载 api 以及遇到的问题
- 【每天记一点】jquery设置radio选中遇到的问题
- PHP,jQuery,JS三种方式设置cookie,获取cookie
- 原生js和jquery中有关透明度设置的相关问题
- 遇到问题-----JS中设置window.location.href跳转无效
- jquery.dragsort.js 实现拖拽过程遇到的问题
- 遇到问题-----JS中设置window.location.href跳转无效(在a标签里或这form表单里)
- jQuery.cookie.js取cookie值为undefined问题
- jquery.cookie.js存与取以及过期时间设置