挺怪的,以前这段代码在Web应用里好好的,到静态页面不行了不知为什么?

解决方案 »

  1.   

    跨域了么 - - 跨域的Cookie不能这么访问
      

  2.   

    跳转前页面URL:
    C:\aa\test\page\inlet\inlet.html跳转后页面URL:
    C:\bb\test\page\introduction\introduction_page.html
      

  3.   

    楼主 需要设置 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>
      

  4.   

    确实是path的问题,谢谢calmcrime。