JS cookies

1655 ワード

Cookieを作成
//   cookie
document.cookie = "username = chen";
//    cookie         (  UTC   GMT   )。     ,cookie          
document.cookie = "username = chen; expires = Thu, 18 Dec 2013 12:00:00 GMT";
//     path         cookie    
document.cookie = "username = chen; expires = Thu, 18 Dec 2013 12:00:00 GMT; path=/";
Cookieの読み込み
var x = document.cookie;
修正Cookie
//   cookie       cookie
document.cookie = "username = chenchen; expires = Thu, 18 Dec 2013 12:00:00 GMT; path=/";
Cookieを削除
//   cookie     。       expires           
document.cookie = "username = chen; expires = Thu, 01 Jan 1970 00:00:00 GMT";
//  ,          cookie   。
JavaScript Cookieの例
1、   cookie     
2、   cookie     
3、   cookie     
完全なインスタンス
//   cookie     
function setCookie(cname,cvalue,exdays){
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
}
//    :
//        ,cookie      cname,cookie     cvalue,     cookie //      expires。
//       cookie  、cookie  、cookie    。


//   cookie     
function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i