日期:2014-05-16  浏览次数:20318 次

一个静态页面将一个值写入Cookie,转到另一个页面怎么读不出来了?
第一个静态页面中是这样写的:
  var selTarget = document.getElementById("roleSelect");
if(selTarget.selectedIndex > -1){
var role=selTarget.value;

setCookie("userRole",role);

location.href="../introduction/introduction_page.html";
}else{
alert("Please select a role!");
}

Cookie的读写函数是这样的:
  function setCookie(cookieName,cookieValue){
var theCookie = cookieName + "=" + cookieValue;

var date = new Date("June 3, 2020");
var cookieDate = date.toGMTString();
theCookie += ";expires=" + cookieDate;

document.cookie = theCookie;
  }


  function getCookie(name){
var cookies = document.cookie.split(";");

for(var i = 0; i < cookies.length; i++){
var cookieCrumbs = cookies[i].split("=");
var cookieName = cookieCrumbs[0];
var cookieValue = cookieCrumbs[1];

if (cookieName == name){
return cookieValue;
}
}

return null;
  };


到了introduction_page.html页面,再使用getCookie取值就去不出来了,怎么回事?

------解决方案--------------------
跨域了么 - - 跨域的Cookie不能这么访问
------解决方案--------------------
楼主 需要设置 cookie path
参考下
http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html
博文的中间 ----- cookie 路径概念


封装了一个cookie

Cme.cookie.js

JScript code

;(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);