Home >Web Front-end >JS Tutorial >JS Add Cookie, Get Cookie Value, Delete Cookie, Detailed Examples_Javascript Skills

JS Add Cookie, Get Cookie Value, Delete Cookie, Detailed Examples_Javascript Skills

WBOY
WBOYOriginal
2016-05-16 16:34:301098browse

Cookies are a very practical function that can determine a certain status and are highly used in projects

Copy code The code is as follows:

Function setCookie(name,value)

var Days = 30;
        var exp = new Date();  
          exp.setTime(exp.getTime() Days*24*60*60*1000);
document.cookie = name "=" escape (value) ";expires=" exp.toGMTString();
        var strsec = getsec(time);  
        var exp = new Date();  
          exp.setTime(exp.getTime() strsec*1);
document.cookie = name "=" escape (value) ";expires=" exp.toGMTString();
}  
//Read cookies
Function getCookie(name)

var arr,reg=new RegExp("(^| )" name "=([^;]*)(;|$)");
If(arr=document.cookie.match(reg))
Return (unescape(arr[2]));
            else  
                 return null;                               }  
//Delete cookies
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();
}  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn