Home  >  Article  >  Web Front-end  >  How to delete javascript cookies

How to delete javascript cookies

藏色散人
藏色散人Original
2021-04-27 09:44:0940536browse

How to delete cookies in javascript: first set the cookie through "setCookie"; then get the cookie through "getCookie"; and finally delete the cookie through "clearCookie".

How to delete javascript cookies

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

JS sets cookies and deletes cookies

There are many ways to set cookies in js.

The first type: (This is the code of w3c official website)

<script>
//设置cookie
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
//获取cookie
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(&#39;;&#39;);
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==&#39; &#39;) c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}
//清除cookie  
function clearCookie(name) {  
    setCookie(name, "", -1);  
}  
function checkCookie() {
    var user = getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
        user = prompt("Please enter your name:", "");
        if (user != "" && user != null) {
            setCookie("username", user, 365);
        }
    }
}
checkCookie(); 
</script>

The second type:

<script>
//JS操作cookies方法!
//写cookies
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());
   }
 
//读取cookies
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
 
    if(arr=document.cookie.match(reg))
 
        return (arr[2]);
    else
        return null;
}
//删除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();
}
//使用示例
setCookie(&#39;username&#39;,&#39;Darren&#39;,30) 
alert(getCookie("username"));
</script>

The third example

<html> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <head> 
        <script language="JavaScript" type="text/javascript"> 
            
            function addCookie(objName, objValue, objHours){//添加cookie 
                var str = objName + "=" + escape(objValue); 
                if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失 
                    var date = new Date(); 
                    var ms = objHours * 3600 * 1000; 
                    date.setTime(date.getTime() + ms); 
                    str += "; expires=" + date.toGMTString(); 
                } 
                document.cookie = str; 
                alert("添加cookie成功"); 
            } 
            
            function getCookie(objName){//获取指定名称的cookie的值 
                var arrStr = document.cookie.split("; "); 
                for (var i = 0; i < arrStr.length; i++) { 
                    var temp = arrStr[i].split("="); 
                    if (temp[0] == objName) 
                        return unescape(temp[1]); 
                } 
            } 
            
            function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间 
                var date = new Date(); 
                date.setTime(date.getTime() - 10000); 
                document.cookie = name + "=a; expires=" + date.toGMTString(); 
            } 
            
            function allCookie(){//读取所有保存的cookie字符串 
                var str = document.cookie; 
                if (str == "") { 
                    str = "没有保存任何cookie"; 
                } 
                alert(str); 
            } 
            
            function $(m, n){ 
                return document.forms[m].elements[n].value; 
            } 
            
            function add_(){ 
                var cookie_name = $("myform", "cookie_name"); 
                var cookie_value = $("myform", "cookie_value"); 
                var cookie_expireHours = $("myform", "cookie_expiresHours"); 
                addCookie(cookie_name, cookie_value, cookie_expireHours); 
            } 
            
            function get_(){ 
                var cookie_name = $("myform", "cookie_name"); 
                var cookie_value = getCookie(cookie_name); 
                alert(cookie_value); 
            } 
            
            function del_(){ 
                var cookie_name = $("myform", "cookie_name"); 
                delCookie(cookie_name); 
                alert("删除成功"); 
            } 
        </script> 
    </head> 
    <body> 
        <form name="myform"> 
            <div> 
                <label for="cookie_name"> 
                    名称 
                </label> 
                <input type="text" name="cookie_name" /> 
            </div> 
            <div> 
                <label for="cookie_value"> 
                值 
                </lable> 
                <input type="text" name="cookie_value" /> 
            </div> 
            <div> 
                <label for="cookie_expireHours"> 
                多少个小时过期 
                </lable> 
                <input type="text" name="cookie_expiresHours" /> 
            </div> 
            <div> 
                <input type="button" value="添加该cookie" onclick="add_()"/><input type="button" value="读取所有cookie" onclick="allCookie()"/><input type="button" value="读取该名称cookie" onclick="get_()"/><input type="button" value="删除该名称cookie" onclick="del_()"/> 
            </div> 
        </form> 
</body> 
</html>

Note:

The chrome browser cannot obtain cookies locally. It must be on the server. If it is local, you can put it under the local www directory.

Google Chrome only supports reading and writing cookies on online websites, and cookie operations on local HTML are prohibited. So if you write the following code in a local html file, the content of the pop-up dialog box will be empty.

document.cookie = "Test=cooo";
alert(document.cookie);

If this page is the content of an online website, the cookie content Test=cooo and so on will be displayed normally.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to delete javascript cookies. For more information, please follow other related articles on the PHP Chinese website!

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