欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript中Cookie操作實(shí)例

 更新時間:2015年01月09日 09:06:02   投稿:junjie  
這篇文章主要介紹了JavaScript中Cookie操作實(shí)例,本文先是講解了Cookie的語法,然后給了實(shí)際操作實(shí)例,需要的朋友可以參考下

JavaScript Cookie

  Cookie對象:
  Cookie是一種以文件的形式保存在客戶端硬盤的Cookies文件夾中的用戶數(shù)據(jù)信息(Cookie數(shù)據(jù))。
  Cookie文件由所訪問的Web站點(diǎn)建立,以長久的保存客戶端與Web站點(diǎn)間的會話數(shù)據(jù),并且該Cookie數(shù)據(jù)只允許被所訪問的Web站點(diǎn)進(jìn)行讀取。
  Cookie文件的格式:
  NS:Cookie.txt
  IE:用戶名@域名.txt

 
  有兩種類型的cookie:
  (1)持久性cookie,會被存儲到客戶端的硬盤上。
  (2)會話Cookie:不會存儲到客戶端的硬盤上,而是放在瀏覽器進(jìn)程所處的內(nèi)存當(dāng)中,當(dāng)瀏覽器關(guān)閉則該會話cookie就銷毀了。

 

用JS實(shí)現(xiàn)Cookie操作

  寫入Cookie:

復(fù)制代碼 代碼如下:

  document.cookie = " 關(guān)鍵字 = 值 [ ; expires = 有效日期 ] [;...]"

  讀取Cookie:
復(fù)制代碼 代碼如下:

  document.cookie

  刪除Cookie:
復(fù)制代碼 代碼如下:

  document.cookie = " 關(guān)鍵字 = ; expires = 當(dāng)前日期"

 

  備注:

  1.有效日期格式:Wdy,DD-Mon-YY HH:MM:SS GMT
  2.Wdy / Mon:英文星期 / 月份;
  3.還包含path、domain、secure屬性;
  4.每個Web站點(diǎn)(domain)可建立20個Cookie數(shù)據(jù);
  5.每個瀏覽器可存儲300個Cookie數(shù)據(jù),4k字節(jié);
  6.客戶有權(quán)禁止Cookie數(shù)據(jù)的寫入。

實(shí)例

復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
  <head>
    <title>cookieTest.html</title>
   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 
  <body>
    <script type="text/javascript">
        var today = new Date();
        var expiredDay = new Date();
        var msPerMonth = 1000 * 60 * 60 * 24 * 30;
       
        expiredDay.setTime(today.getTime() + msPerMonth); //一個月之后過期
       
        //寫入cookie
        document.cookie = "name=mengdd;expires="+expiredDay.toGMTString();
       
        document.writeln("cookie已經(jīng)寫到硬盤上了");
       
        //讀取cookie
        document.writeln("內(nèi)容是:" + document.cookie);
        document.writeln("expire day: " + expiredDay.toGMTString());
    </script>
  </body>
</html>


相關(guān)文章

最新評論