js取得指定のクッキーの具体的な実現

1098 ワード

 
  
var cookieName = "cookie ";
var cookieValue = null;// cookie value
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');// cookie
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];// cookies
if (cookie.substring(0, cookieName.length + 2).trim() == cookieName.trim() + "=") {// cookie cookie
cookieValue = cookie.substring(cookieName.length + 2, cookie.length);
break
}
}
}
if (cookieValue != "" && cookieValue != null) {// cookie
alert(cookieValue);
} else {// cookie
alert("not cookie!!!");
}
 
  
//
String.prototype.trim = function () {
return this.replace(new RegExp("(^[\\s]*)|([\\s]*$)", "g"), "");
}