本地创建一个测试a.html文件,用ifame包含一个其他域名下有cookie操作的文件b.html当直接访问b.html时,所有cookie操作均有效。
当访问a.html时,iframe中b.html的getcookie,deletecookie都有效,setcookie失效。测试环境包括 ie6,ie7,ie8,firefox3.5,其中ie8,firefox3.5正常,ie6,ie7出现以上问题。请知道的给予解答。
谢谢先
当访问a.html时,iframe中b.html的getcookie,deletecookie都有效,setcookie失效。测试环境包括 ie6,ie7,ie8,firefox3.5,其中ie8,firefox3.5正常,ie6,ie7出现以上问题。请知道的给予解答。
谢谢先
<head>
<title></title>
</head>
<body>
<iframe src="http://www.xxx.com/b.html"></iframe>
</body>
</html>
b.html<html>
<head>
<title></title>
</head>
<body>
<script language="javascript">
function xcookie(name, value, options) {
if (typeof value != 'undefined') {
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 {
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]);
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
var n = "test";
var v = parseInt(xcookie(n));
if (v==null || isNaN(v)) v = 0;
xcookie(n, v+1);
document.write(v+1);
</script>
</body>
</html>
<head>
<title></title>
<script type="text/javascript" src="js/jquery.js"></script>
</head><body>
<script language="javascript">
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') {
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 {
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]);
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};var n = "test";
var v = parseInt($.cookie(n));
if (v==null || isNaN(v)) v = 0;
$.cookie(n, v+1);
document.write(v+1);</script>
</body>
</html>
options.domain 值为:xxx.com把这个参数加上呢?或加:
window.domain="xxx.com";
应该是:
document.domain='xxx.com';最好设置cookie的时候也:
$.cookie(n, v+1,{path:'/',domain:'xxx.com'});
但是.php中在html代码输出之前(也就是<html>标签之前) header('P3P: CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"'); 是能够解决问题但在html文件中head内加入 <meta http-equiv="P3P" content='CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"'> 就无效
谁能告诉我在html中怎么加。
因为它需要在http请求的头部写入内容
也就是说,这个东西就是解决ie6,7对于跨域setcookie的。
但网上说html中可以在head中加那一段代码实现,可是我测试发现不行郁闷。谁再给看看?:)
我现在的问题就是在静态页面html中如何写才能达到同样的效果。或者是没有办法做到?。。
js就不知道了, js 本身就受限于各种安全,很少用如果能用就是跨域了理论上,只你的浏览器允许跨域(选项里可以设置),或许可以;一般而言,应是不允许跨域
只是相对于top而言iframe中的cookie操作跨域。不是js操作,是html代码中,head部分的mata属性中添加这个。。
我错了。网上有很多解决办法。
你去搜一下
首先保存cookie不能设置过期时间,和其他什么项。
还要结合P3P来。有一种静态文件下解决这个的方法,是要建立个文件,然后在页面里<link.....>它。
具体的去搜搜吧。也有不用这种的,不过步骤更麻烦,利用的脚本攻击的方式了。
再等等看有没有人有不同看法,如果没有,1天后结贴