萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> js獲取指定的cookie的具體實現

js獲取指定的cookie的具體實現

 使用js獲取cookie或許你會,但是要獲取指定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 < 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 {<span style="font-family: Arial, Helvetica, sans-serif;">//如果cookie的值是空</span>  alert("not cookie!!!");  }   代碼如下: //去除空格  String.prototype.trim = function () {  return this.replace(new RegExp("(^[s]*)|([s]*$)", "g"), "");  }   
copyright © 萬盛學電腦網 all rights reserved