JS設定cookie、讀取cookie、刪除cookie

來源:互聯網
上載者:User

標籤:time   res   cap   注意   font   預設   blog   ext   cookie   

JS設定cookie、讀取cookie、刪除cookie

 

     

 JS設定cookie,注意一定要path=/ ,根目錄,不然其他目錄可能查詢不到。。預設是本目錄。

   document.cookie = 不會吧原cookie刪除的,可能是個屬性實現模式,實際是個增量過程。。

刪除cookie,通過是指到期時間來進行。。。

 

 

function writeCookie(name, value, hours)

 

{

 

  var expire = "";

 

  if(hours != null)

 

  {

 

    expire = new Date((new Date()).getTime() + hours * 3600000);

 

    expire = "; expires=" + expire.toGMTString();

 path="; path=/";

 

  }

 

  document.cookie = name + "=" + escape(value) + expire+path;

 

}

 

function setCookie(name, value, expires, path, domain, secure)

    {

        document.cookie = name + "=" + escape(value) +

            ((expires) ? "; expires=" + expires : "") +

            ((path) ? "; path=" + path : "") +

            ((domain) ? "; domain=" + domain : "") +

            ((secure) ? "; secure" : "");

}

 

 

刪除cookies

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

function delCookie(name)

{

var exp = new Date();

exp.setTime(exp.getTime() - 1);

var cval=getCookie(name);

if(cval!=null)

document.cookie= name + "="+cval+";expires="+exp.toGMTString();

}

//使用樣本

setCookie("name","hayden");

alert(getCookie("name"));

//如果需要設定自訂到期時間

//那麼把上面的setCookie 函數換成下面兩個函數就ok;

//程式碼

 

 

 

作者:: 綽號:老哇的爪子 ( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿爾 拉帕努伊 ) 

漢字名:艾提拉(艾龍),   EMAIL:[email protected]

轉載請註明來源: http://www.cnblogs.com/attilax/

Atiend

 

 

 

JS設定cookie、讀取cookie、刪除cookie

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.