本地创建一个测试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出现以上问题。请知道的给予解答。
谢谢先

解决方案 »

  1.   

    a.html<html>
    <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>
      

  2.   

    b.html 是这个。。<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>
      

  3.   

    options.path 值为:/
    options.domain 值为:xxx.com把这个参数加上呢?或加:
    window.domain="xxx.com";
      

  4.   

    http://hi.baidu.com/houdelei250/blog/item/da1ad135f0c39147251f14bd.htmlheader("P3P: CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");看看上面的那个链接
      

  5.   

    可以的,我以前做过类似的。
    应该是:
    document.domain='xxx.com';最好设置cookie的时候也:
    $.cookie(n, v+1,{path:'/',domain:'xxx.com'});
      

  6.   

    真是越做越回去了应该是P3P可以解决的。
    但是.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中怎么加。
      

  7.   

    是不是html静态页面中设置不了?
    因为它需要在http请求的头部写入内容
      

  8.   

    没测过,但据康盛的工程师介绍POP在做跨域时,setcookie在不同浏览器有这种问题,无效。但它说的相反,好象是IE8下无效。这可能和浏览器安全相关。
      

  9.   

    p3p,firefox等是不支持的,同样,对于跨域的cookie访问,firefox默认的配置中也是允许的。
    也就是说,这个东西就是解决ie6,7对于跨域setcookie的。
    但网上说html中可以在head中加那一段代码实现,可是我测试发现不行郁闷。谁再给看看?:)
      

  10.   

    js可能存在问题,能不能跨域不知道。但如uncenter的那个,是由header在php中输出的,而且可能是在所有输出之前测下看看
      

  11.   

    是的,我上面说了,php是在所有输出之前,所以它行。没问题
    我现在的问题就是在静态页面html中如何写才能达到同样的效果。或者是没有办法做到?。。
      

  12.   

    是不是机器今天或者昨天大漏洞来,我用jquery中的cookie插件ff好用,但是ie7就不能用了,头两天做了个本地图片预览,昨天就不好用了(ie7),之前就是打了几个漏洞
      

  13.   


    js就不知道了, js 本身就受限于各种安全,很少用如果能用就是跨域了理论上,只你的浏览器允许跨域(选项里可以设置),或许可以;一般而言,应是不允许跨域
      

  14.   

    这个跨域是指iframe加在不同域名的页面,在该页面的cookie操作其实是不是跨域的。
    只是相对于top而言iframe中的cookie操作跨域。不是js操作,是html代码中,head部分的mata属性中添加这个。。
      

  15.   

    测试了一下。
    我错了。网上有很多解决办法。
    你去搜一下
    首先保存cookie不能设置过期时间,和其他什么项。
    还要结合P3P来。有一种静态文件下解决这个的方法,是要建立个文件,然后在页面里<link.....>它。
    具体的去搜搜吧。也有不用这种的,不过步骤更麻烦,利用的脚本攻击的方式了。
      

  16.   

    ...也就是html下没有常规解决办法了?那算了
    再等等看有没有人有不同看法,如果没有,1天后结贴