JavaScript Deleting a Cookie

In the preceding part of this guide, we were introduced to various methods for establishing and modifying a cookie in JavaScript. In addition to those operations, JavaScript provides functionality for removing a cookie. Now, let's explore the different approaches available for deleting a cookie.

Different ways to delete a Cookie

These are the following ways to delete a cookie:

  • A cookie can be deleted by using expire attribute.
  • A cookie can also be deleted by using max-age attribute.
  • We can delete a cookie explicitly, by using a web browser.
  • Examples to delete a Cookie

    Example 1

In this instance, we employ the expire attribute to remove a cookie by assigning a past date as the expiry date for it.

Example

<!DOCTYPE html>

<html>

<head>

</head>

<body>

   

<input type="button" value="Set Cookie" onclick="setCookie()">

<input type="button" value="Get Cookie" onclick="getCookie()">

<script>

function setCookie() 

{

    document.cookie="name=Martin Roy; expires=Sun, 20 Aug 2000 12:00:00 UTC";

  

} 

function getCookie()

{

    if(document.cookie.length!=0)

    {

    alert(document.cookie);

    }

    else

    {

        alert("Cookie not avaliable");

    }

}

</script>

</body>

</html>

Example 2

In this instance, we utilize the max-age parameter to remove a cookie by assigning a value of zero or a negative number (in seconds) to it.

Example

<!DOCTYPE html>

<html>

<head>

</head>

<body>

   

<input type="button" value="Set Cookie" onclick="setCookie()">

<input type="button" value="Get Cookie" onclick="getCookie()">

<script>

function setCookie() 

{

    document.cookie="name=Martin Roy;max-age=0";

} 

function getCookie()

{

    if(document.cookie.length!=0)

    {

    alert(document.cookie);

    }

    else

    {

        alert("Cookie not avaliable");

    }

}



</script>

</body>

</html>

Example 3

An illustration will be presented to demonstrate how to establish, retrieve, and remove multiple cookies.

Example

<!DOCTYPE html>

<html>

<head>

</head>

<body>

   

<input type="button" value="Set Cookie1" onclick="setCookie1()">

<input type="button" value="Get Cookie1" onclick="getCookie1()">

<input type="button" value="Delete Cookie1" onclick="deleteCookie1()">

<br>

<input type="button" value="Set Cookie2" onclick="setCookie2()">

<input type="button" value="Get Cookie2" onclick="getCookie2()">

<input type="button" value="Delete Cookie2" onclick="deleteCookie2()">

<br>

<input type="button" value="Display all cookies" onclick="displayCookie()">



<script>

function setCookie1() 

{

    document.cookie="name=Martin Roy";

     cookie1=  document.cookie;

} 

function setCookie2() 

{

    document.cookie="name=Duke William";

     cookie2=  document.cookie;

} 



function getCookie1()

{

    if(cookie1.length!=0)

    {

    alert(cookie1);

    }

    else

    {

        alert("Cookie not available");

    }

}



function getCookie2()

{

    if(cookie2.length!=0)

    {

    alert(cookie2);

    }

    else

    {

        alert("Cookie not available");

    }

}



function deleteCookie1()

{

    document.cookie=cookie1+";max-age=0";

    cookie1=document.cookie;

    alert("Cookie1 is deleted");

}



function deleteCookie2()

{

    document.cookie=cookie2+";max-age=0";

    cookie2=document.cookie;

   alert("Cookie2 is deleted");

}



function displayCookie()

{

if(cookie1!=0&&cookie2!=0)

{

    alert(cookie1+" "+cookie2);

}

else if(cookie1!=0)

{

    alert(cookie1);

}

else if(cookie2!=0)

{

    alert(cookie2);

}

else{

    alert("Cookie not available");

}



}



</script>

</body>

</html>

Example 4

Let's explore an instance where we specifically remove a cookie.

Example

<!DOCTYPE html>

<html>

<head>

</head>

<body>

   

<input type="button" value="Set Cookie" onclick="setCookie()">

<input type="button" value="Get Cookie" onclick="getCookie()">

<script>

function setCookie() 

{

    document.cookie="name=Martin Roy";

  

} 

function getCookie()

{

    if(document.cookie.length!=0)

    {

    alert(document.cookie);

    }

    else

    {

        alert("Cookie not avaliable");

    }

}

</script>

</body>

</html>

Upon clicking the "Set Cookie" button for the first time, subsequent clicks on the "Get Cookie" button will reveal the key and value of the cookies on the screen.

To explicitly remove a cookie, adhere to the subsequent instructions:

  • Launch Mozilla Firefox.
  • Navigate to Open menu, then Library, followed by History, Clear Recent History, and finally Details.

[visual]

  • Displayed above is a pre-selected Cookies checkbox. Proceed by selecting Clear Now to explicitly remove the cookies.

Upon clicking the "Get Cookie" button, the following dialog box will be displayed.

Here, we can see that the cookies are deleted.

Input Required

This code uses input(). Please provide values below: