【Javascript】javascript學習 三十一 JavaScript Cookies

來源:互聯網
上載者:User

cookie 用來識別使用者。

執行個體
建立一個歡迎 cookie
利用使用者在提示框中輸入的資料建立一個 JavaScript Cookie,當該使用者再次訪問該頁面時,根據 cookie 中的資訊發出歡迎資訊。
什麼是cookie?

cookie 是儲存於訪問者的電腦中的變數。每當同一台電腦通過瀏覽器請求某個頁面時,就會發送這個 cookie。你可以使用 JavaScript 來建立和取回 cookie 的值。

有關cookie的例子:
名字 cookie
當訪問者首次訪問頁面時,他或她也許會填寫他/她們的名字。名字會儲存於 cookie 中。當訪問者再次訪問網站時,他們會收到類似 "Welcome John Doe!" 的歡迎詞。而名字則是從 cookie 中取回的。
密碼 cookie
當訪問者首次訪問頁面時,他或她也許會填寫他/她們的密碼。密碼也可被儲存於 cookie 中。當他們再次訪問網站時,密碼就會從 cookie 中取回。
日期 cookie
當訪問者首次訪問你的網站時,當前的日期可儲存於 cookie 中。當他們再次訪問網站時,他們會收到類似這樣的一條訊息:"Your last visit was on Tuesday August 11, 2005!"。日期也是從 cookie 中取回的。
建立和儲存 cookie

在這個例子中我們要建立一個儲存訪問者名字的 cookie。當訪問者首次訪問網站時,他們會被要求填寫姓名。名字會儲存於 cookie 中。當訪問者再次訪問網站時,他們就會收到歡迎詞。

首先,我們會建立一個可在 cookie 變數中儲存訪問者姓名的函數:

function setCookie(c_name,value,expiredays){var exdate=new Date()exdate.setDate(exdate.getDate()+expiredays)document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())}

上面這個函數中的參數存有 cookie 的名稱、值以及到期天數。

在上面的函數中,我們首先將天數轉換為有效日期,然後,我們將 cookie 名稱、值及其到期日期存入 document.cookie 對象。

之後,我們要建立另一個函數來檢查是否已設定 cookie:

function getCookie(c_name){if (document.cookie.length>0)  {  c_start=document.cookie.indexOf(c_name + "=")  if (c_start!=-1)    {     c_start=c_start + c_name.length+1     c_end=document.cookie.indexOf(";",c_start)    if (c_end==-1) c_end=document.cookie.length    return unescape(document.cookie.substring(c_start,c_end))    }   }return ""}

上面的函數首先會檢查 document.cookie 對象中是否存有 cookie。假如 document.cookie 對象存有某些 cookie,那麼會繼續檢查我們指定的 cookie 是否已儲存。如果找到了我們要的 cookie,就傳回值,否則返回Null 字元串。

最後,我們要建立一個函數,這個函數的作用是:如果 cookie 已設定,則顯示歡迎詞,否則顯示提示框來要求使用者輸入名字。

function checkCookie(){username=getCookie('username')if (username!=null && username!="")  {alert('Welcome again '+username+'!')}else   {  username=prompt('Please enter your name:',"")  if (username!=null && username!="")    {    setCookie('username',username,365)    }  }}

這是所有的代碼:

<html><head><script type="text/javascript">function getCookie(c_name){if (document.cookie.length>0)  {  c_start=document.cookie.indexOf(c_name + "=")  if (c_start!=-1)    {     c_start=c_start + c_name.length+1     c_end=document.cookie.indexOf(";",c_start)    if (c_end==-1) c_end=document.cookie.length    return unescape(document.cookie.substring(c_start,c_end))    }   }return ""}function setCookie(c_name,value,expiredays){var exdate=new Date()exdate.setDate(exdate.getDate()+expiredays)document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())}function checkCookie(){username=getCookie('username')if (username!=null && username!="")  {alert('Welcome again '+username+'!')}else   {  username=prompt('Please enter your name:',"")  if (username!=null && username!="")    {    setCookie('username',username,365)    }  }}</script></head><body onLoad="checkCookie()"></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.