Skip to content
the poor coder

jquery delete a cookie

Poor Man
Published
Reading time
1 min read
On this page

Cookies are small data files that websites save on your computer or mobile device. They store information such as login credentials or user preferences. Sometimes you may want to delete a cookie using JQuery.

The easiest way to delete a cookie using JQuery is by using the jQuery Cookie plugin. This plugin provides a convenient way to create, read, and delete cookies in JQuery.

Here is the syntax to delete a cookie using the jQuery Cookie plugin:

Text

    $.removeCookie('cookie_name');

Make sure to replace 'cookie_name' with the name of the cookie that you want to delete.

Method 2: Using Vanilla JavaScript#

If you don't want to use a plugin, you can still delete a cookie using vanilla JavaScript.

Here is the syntax to delete a cookie using vanilla JavaScript:

Text

    document.cookie = 'cookie_name=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

Make sure to replace 'cookie_name' with the name of the cookie that you want to delete.

Explanation:#

The first method uses the jQuery Cookie plugin to delete a cookie. The $.removeCookie() function provided by the plugin takes the name of the cookie that you want to delete as its argument. This function sets the value of the cookie to null and sets its expiration date to a date in the past, effectively deleting the cookie.

The second method uses vanilla JavaScript to delete a cookie. The code sets the value of the cookie to an empty string and sets its expiration date to a date in the past. The cookie is effectively deleted when the browser processes the cookie string.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC