2009-09-21 38 views
1

這是我的asp代碼,可以在Javascript中完成相同的操作嗎?在javascript中讀取cookie

HttpCookie cookie = this.Request.Cookies["Votes"]; 
if (cookie != null) 
    if (cookie.Values.Get(id.ToString()) == "true") return true; 
return false; 

回答

6
function readTheCookie(the_info) 
{ 
// load the cookie into a variable and unescape it 

var the_cookie = document.cookie; 
var the_cookie = unescape(the_cookie); 

// separate the values from the cookie name 

var broken_cookie = the_cookie.split("some parameter"); // parameter depends on how the cookie is stored 
var the_values = broken_cookie["some index"]; // index of the value that you want 
} 

這些都是讀一個cookie的所有部分,你可以使用這個片段來實現你想要的。

+0

VAR the_cookie = UNESCAPE(the_cookie)剛剛返回 「投票」 沒有任何價值發現這個代碼。 – dani 2009-09-21 14:41:07

+0

而var the_cookie = document.cookie;返回: the_cookie =「Votes = 47 = true&31 = true&48 = true」 – dani 2009-09-21 14:42:25

+0

ans您在哪裏使用the_info參數? – dani 2009-09-21 14:44:52

3

document.cookie使您能夠訪問JavaScript中的cookie。你需要做一些解析來做你想做的事情。

1

使用此代碼

function setCookie(c_name, value, exdays) { 
      var exdate = new Date(); 
      exdate.setDate(exdate.getDate() + exdays); 
      var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); 
      document.cookie = c_name + "=" + c_value; 
     } 


function getCookie(c_name) { 
    var i, x, y, ARRcookies = document.cookie.split(";"); 
    for (i = 0; i < ARRcookies.length; i++) { 
     x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("=")); 
     y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1); 
     x = x.replace(/^\s+|\s+$/g, ""); 
     if (x == c_name) { 
      return unescape(y); 
     } 
    } 
} 

function GetSetCookie() { 
      var version = getCookie("version"); 
      if (version != null && version != "") { 
       if (version == 'full') { 
        version = 'text'; 
       } 
       else { 
        version = 'full'; 
       } 
      } 
      else { 
       version = 'full'; 
      } 
      setCookie("version", version, 365); 
      window.top.location.reload(); 
     } 
+0

你是否從http://www.w3schools.com/js/js_cookies.asp複製了這個內容? 如果是這樣,你應該透露它。 – Saurav 2012-03-29 00:32:34

0

我在W3schools這對我的作品

function setCookie(cname, cvalue, exdays) { 
     var d = new Date(); 
     d.setTime(d.getTime() + (exdays*24*60*60*1000)); 
     var expires = "expires="+d.toUTCString(); 
     document.cookie = cname + "=" + cvalue + "; " + expires; 
    } 

function getCookie(cname) { 
    var name = cname + "="; 
    var ca = document.cookie.split(';'); 
    for(var i=0; i<ca.length; i++) { 
     var c = ca[i]; 
     while (c.charAt(0)==' ') c = c.substring(1); 
     if (c.indexOf(name) == 0) return c.substring(name.length, c.length); 
    } 
    return ""; 
} 

function checkCookie() { 
    var user = getCookie("username"); 
    if (user != "") { 
     alert("Welcome again " + user); 
    } else { 
     user = prompt("Please enter your name:", ""); 
     if (user != "" && user != null) { 
      setCookie("username", user, 365); 
     } 
    } 
}