Deleting a Cookie in JavaScriptIn the previous section, we learned the different ways to set and update a cookie in JavaScript. Apart from that, JavaScript also allows us to delete a cookie. Here, we see all the possible ways to delete a cookie. Different ways to delete a CookieThese are the following ways to delete a cookie:
Examples to delete a CookieExample 1In this example, we use expire attribute to delete a cookie by providing expiry date (i.e. any past date) to it. Example 2In this example, we use max-age attribute to delete a cookie by providing zero or negative number (that represents seconds) to it. Example 3Let's see an example to set, get and delete multiple cookies. Example 4Let's see an example to delete a cookie explicitly. After clicking Set Cookie once, whenever we click Get Cookie, the cookies key and value is displayed on the screen. To delete a cookie explicitly, follow the following steps:
Now, on clicking Get Cookie, the below dialog box appears. Here, we can see that the cookies are deleted.
Next TopicJavaScript Events
|