DIV CSS 佈局教程網

JavaScript Cookie 實戰:設定、讀取、刪除的完整範例與眉角
編輯:關於JavaScript     

整理了一些 JavaScript 操作 Cookie 的文章,這篇的寫法相當值得參考。它唯一的不足是對路徑(path)的處理,實際使用時建議自行補強。

最精簡的三支函式

<script>
function SetCookie(name, value) //兩個參數:cookie 名與值
{
  var Days = 30; //此 cookie 將被保存 30 天
  var exp = new Date();
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getCookie(name) //取 cookies
{
  var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  if (arr != null) return unescape(arr[2]);
  return null;
}

function delCookie(name) //刪除 cookie
{
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

SetCookie("xiaoqi", "3");
alert(getCookie('xiaoqi'));
</script>

功能更完整的版本:可指定 expires、path、domain、secure

function GetCookieVal(offset) //獲得 Cookie 解碼後的值
{
  var endstr = documents.cookie.indexOf(";", offset);
  if (endstr == -1) endstr = documents.cookie.length;
  return unescape(documents.cookie.substring(offset, endstr));
}

function SetCookie(name, value) //設定 Cookie 值
{
  var expdate = new Date();
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path    = (argc > 3) ? argv[3] : null;
  var domain  = (argc > 4) ? argv[4] : null;
  var secure  = (argc > 5) ? argv[5] : false;
  if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
  documents.cookie = name + "=" + escape(value)
    + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString()))
    + ((path   == null) ? "" : ("; path=" + path))
    + ((domain == null) ? "" : ("; domain=" + domain))
    + ((secure == true) ? "; secure" : "");
}

function GetCookie(name) //獲得 Cookie 的原始值
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = documents.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (documents.cookie.substring(i, j) == arg) return GetCookieVal(j);
    i = documents.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function DelCookie(name) //刪除 Cookie
{
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = GetCookie(name);
  documents.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

應用:用 Cookie 記住「已看過公告」

<SCRIPT language="javascript">
<!--
function openpopup() {
  window.open("gonggao.htm", "gonggao", "width=260,height=212,left=200,top=0");
}
function get_cookie(Name) {
  var search = Name + "=";
  var returnvalue = "";
  if (documents.cookie.length > 0) {
    offset = documents.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = documents.cookie.indexOf(";", offset);
      if (end == -1) end = documents.cookie.length;
      returnvalue = unescape(documents.cookie.substring(offset, end));
    }
  }
  return returnvalue;
}
function helpor_net() {
  if (get_cookie('popped') == '') {
    openpopup();
    documents.cookie = "popped=yes";
  }
}
helpor_net()
//-->
</SCRIPT>

只要使用者按過確定,之後不再清空 Cookie 就不會重複提示;不按確定則每次進站都會再問一次。把這段放進外部 js 檔全站引用即可。

兩個容易踩到的兼容性提醒

Cookie 有兩個版本。Version 0 由 Netscape 制定,幾乎所有瀏覽器都支援;Version 1 依 RFC 2109 制定,目前 Java 的 javax.servlet.http.Cookie 尚未支援新版。為了相容性,涉及 Cookie 的操作最好都針對舊版進行。

內容字元有限制。在 Version 0 中,空格、方括號、圓括號、等號、逗號、雙引號、斜線、問號、@、冒號、分號都不能作為 Cookie 內容。Version 1 雖然放寬了限制,但考量到新版規範尚未被全面支援,保險起見還是應避免在內容中使用這些字元。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved