var cookieName = "cookie";
var cookieName = "cookie";
var cookieValue = null;//傳回cookie的value值
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');//將取得的所有cookie切成數組
for (var i = 0; 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"), ""); }