初學Javascript之cookie篇(譯)

來源:互聯網
上載者:User
CSDN - 文檔中心 - Javascript    
標題     初學Javascript之cookie篇(譯)    yjgx007(翻譯)
關鍵字     cookie 樣本 原始碼
出處     http://www.codeproject.com/jscript/cookies_intro.asp


著作權聲明:可以任意轉載,轉載時請務必以超連結形式標明文章原始出處http://xinyistudio.vicp.net/和作者資訊及本聲明

[譯者註:將本文的最後範例程式碼拷貝至文字檔中,更名檔案為homepage.htm格式檔案,在瀏覽器中運行,本文以該範例程式碼講解]

簡介

=================================================================

問題是我想解決自動訪問http://www.thehungersite.com/。這個頁面能夠限制你每一天只訪問它一次(不要忘記在這連結上點擊)。
現在為止,每次一開始我做的第一件事就是手工選擇我的書籤載入這個頁面。

為什麼不為它建立一個簡單的指令碼程式呢?
因為我想讓它在Netscape和IE下都能夠同樣的工作,我開始學習Javascript。

 

解決

=================================================================

主題思想很簡單:建立一個頁面並測試一下今天這個頁面是否已被載入過,
如果沒有載入,那就通過它連結到http://www.thehungersite.com/,並且設定
這個頁面作為瀏覽器的首頁。

擷取頁面並重新導向是很容易的,問題是如何記憶這個頁面已被訪問過。

因為Javascript沒有檔案訪問的功能,看來我們只能使用cookies了。

Cookies是一個有大小限制的變數,它與一個伺服器的網域名稱相關聯,
預設情況下cookie的生存期是當瀏覽器關閉時被清空(注意:不是當你離開
這個頁面的時候),但可以用一個指令碼程式改變這種情況,
在使用者關閉瀏覽器後使cookies能夠儲存下來,Netscape在檔案中使用所有的
Cookie,而IE分別儲存每個cookie。此外,不同的瀏覽器會帶來一些意想
不到的情況,你必須確定一個使用者在它的瀏覽器設定中是否關閉了cookies。

一切都很好也很妙,只是現在我還未在IE中測試它,調用樣本Javascript語句:
cookieExpires = "01-APR-" + nLyear + " GMT";
document.cookies = cookieName + "=" + cookieValue + ";  expires=" + cookieExpires;

然後調用
document.write(document.cookie);

document.cookie是空的。

-------------------------------------------------------------------------------------------------------

在實驗和研究了一下上面的樣本程式後,會發現:

1。你不能夠讀和顯示cookie。如果你想看一下這個cookie你需要指定與它相同的另一個字串變數,如下:
   document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
   myvar = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
   document.write(myvar);

 2. 瀏覽器用了不同的日期格式:
    Netscape使用"GMT"結束,IE使用“UTC",這是因為它可以更好的構建一個日期,象下面這樣:
      var expdate = new Date()
   cookieExpires.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)) //+1 day
   cookieExpires.toGMTString()

   當你顯示日期部分
   document.write(expdate.getYear() + "<br>" + expdate.getMonth() + "<br>" + expdate.getDate());
   對於2000-11-15的日期,在IE中顯示為2000/10/15,在Netscape下顯示100/10/15(註:已確定是一些較低版本的
   Netscape 瀏覽器的Y2K問題)

   樣本中看到像下面這樣的部分:
   if (platform == "Mac") {
 lastVisit = lastVisit - (24 * 60 * 60 * 1000)
   }
   但我不可能檢測它。

日期對象有getDate和getDay的方法,第二個方法返回在一周中天的索引號。

-------------------------------------------------------------------------------------------------------
知道了這些,基本上就沒問題了(現在你可以看一下homepage.htm)

[譯者註:將本文的最後範例程式碼拷至文字檔中並儲存htm格式,然後運行]

最後要說明的是,這不僅是一個專用的JS指令碼,如果你想將它用在你的web頁面上你必須最小程度的使用不同的
瀏覽器測試它並注意它們的版本,許多的指令碼程式包含了瀏覽器類型檢測和大量的if...else語句,以處理這樣不同。

 

樣本頁面homepage.htm原始碼

===============================================================

<html>
<head>
<title>Homepage</title>
</head>
<body>
<a href='http://www.thehungersite.com/'>Manual redirection<br></a>
<a href="javascript:ResetCookie()">Cookie reset</a>

<script language="JavaScript">
<!--
var bVisitedToday = false;

var lastVisit = GetCookie("lastVisit");
if (lastVisit != null)
{
  lastVisit = 1 * lastVisit;
  var lastHere = new Date(lastVisit); 
  var rightNow = new Date();

  if(lastHere.getYear() == rightNow.getYear()
     && lastHere.getMonth() == rightNow.getMonth()
     && lastHere.getDate() == rightNow.getDate())
  {
     bVisitedToday = true;
  }
}

if(bVisitedToday == false)
{
  setLastlastVisitCookie();
  window.location="http://www.thehungersite.com/"
}
else
{
  //window.location="about:blank"
}

function getCookieVal (offset)
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name)
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
function SetCookie (name, value)
{
  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;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}
function setLastlastVisitCookie ()
{
  var rightNow = new Date();
  var expdate = new Date();
  expdate.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)); //+1 day
  SetCookie ("lastVisit", rightNow.getTime(), expdate, "/");
}
function ResetCookie()
{
  SetCookie("lastVisit", 0, null, "/");
}
// -->
</script>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.