一个静态页面将一个值写入Cookie,转到另一个页面怎么读不出来了? 挺怪的,以前这段代码在Web应用里好好的,到静态页面不行了不知为什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 跨域了么 - - 跨域的Cookie不能这么访问 跳转前页面URL:C:\aa\test\page\inlet\inlet.html跳转后页面URL:C:\bb\test\page\introduction\introduction_page.html 楼主 需要设置 cookie path参考下http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html博文的中间 ----- cookie 路径概念封装了一个cookieCme.cookie.js;(function(w, d){ var cookie = function(name, value, options) { if (typeof value != 'undefined') { // name和value有值, 那么设置 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(); } 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 { // 只有name值 那么获取 cookie var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = trim(cookies[i]); if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } }; function trim(v){ return v.replace(/^\s|\s$/, ''); } w.Cme ? '' : w.Cme = {}; w.Cme.cookie = cookie;})(window, document);Cme.cookie.html<!DOCTYPE HTML><html> <head> <meta charset="gb2312" /> <title></title> <style> div { margin:10px; width:200px; height:200px; border:1px solid #000; } </style> </head> <body> <div></div> <button id="a">设置cookie</button> <button id="b">获取cookie</button> <button id="c">删除cookie</button> <script src="cme.cookie.js"></script> <script> function $(el){ return typeof el == 'string' ? document.getElementById(el) : el; } function $t(name, cot){ cot = cot || document; return cot.getElementsByTagName(name); } /* // cookie 名称 var cookie = 'my_skin'; // 设置 cookie 值 var cookie_test = 'some cookie'; // 设置cookie 天数 $.cookie(cookie, cookie_test, { path: '/', expires: 10 }); // 设置cookie 时间 var date = new Date(); date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000)); $.cookie(cookie, cookie_test, { path: '/', expires: date }); // 获取 cookie 值 alert( $.cookie(cookie) ) // 删除 cookie $.cookie(cookie, null, { path: '/' }); */ var c = Cme.cookie('skin'); if(c){ $t('div')[0].style.border = '1px solid red' } $('a').onclick = function(){ var date = new Date(); date.setTime(date.getTime() + (4 * 1000)); // 4秒 cookie Cme.cookie('skin', 'red', { path: '/', expires: date }); } $('b').onclick = function(){ var c = Cme.cookie('skin'); alert(c) } $('c').onclick = function(){ Cme.cookie('skin', null, { path: '/' }) } </script> </body></html> 确实是path的问题,谢谢calmcrime。 求javascript权威指南第5版PDF格式 js显示网站问题 file:///访问的问题 我希望将页面中的<font color="red">替换成<font color="blue">,怎么办? 注册时验证重新输入的密码是否正确 如何获得变量头3个字母? jsp的checkbox问题 如何强制包含 禁止被框架包含的页面 companyRadioObj.length为什么在Mozzila应用不正确? 寻求分页打印良药,本人已经病入膏肓了!救小弟一命! 正则表达式 最短匹配问题 javascript和java中“对象”概念的比较
C:\aa\test\page\inlet\inlet.html跳转后页面URL:
C:\bb\test\page\introduction\introduction_page.html
参考下
http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html
博文的中间 ----- cookie 路径概念
封装了一个cookieCme.cookie.js
;(function(w, d){
var cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name和value有值, 那么设置 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();
}
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 { // 只有name值 那么获取 cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = trim(cookies[i]);
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
function trim(v){
return v.replace(/^\s|\s$/, '');
}
w.Cme ? '' : w.Cme = {};
w.Cme.cookie = cookie;
})(window, document);
Cme.cookie.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
div {
margin:10px;
width:200px; height:200px;
border:1px solid #000;
}
</style>
</head>
<body>
<div></div>
<button id="a">设置cookie</button>
<button id="b">获取cookie</button>
<button id="c">删除cookie</button>
<script src="cme.cookie.js"></script>
<script>
function $(el){
return typeof el == 'string' ? document.getElementById(el) : el;
}
function $t(name, cot){
cot = cot || document;
return cot.getElementsByTagName(name);
}
/*
// cookie 名称
var cookie = 'my_skin';
// 设置 cookie 值
var cookie_test = 'some cookie';
// 设置cookie 天数
$.cookie(cookie, cookie_test, { path: '/', expires: 10 });
// 设置cookie 时间
var date = new Date();
date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
$.cookie(cookie, cookie_test, { path: '/', expires: date });
// 获取 cookie 值
alert( $.cookie(cookie) )
// 删除 cookie
$.cookie(cookie, null, { path: '/' });
*/
var c = Cme.cookie('skin');
if(c){
$t('div')[0].style.border = '1px solid red'
}
$('a').onclick = function(){
var date = new Date();
date.setTime(date.getTime() + (4 * 1000)); // 4秒 cookie
Cme.cookie('skin', 'red', { path: '/', expires: date });
}
$('b').onclick = function(){
var c = Cme.cookie('skin');
alert(c)
}
$('c').onclick = function(){
Cme.cookie('skin', null, { path: '/' })
}
</script>
</body>
</html>