document.cookie
PropertyYou can use the JS document.cookie
property to set/unset a cookie, you don't need jQuery for this.
Let's take a closer look at the following example to understand how to set, read, update, unset or delete a cookie associated with the document in JavaScript:
<script>
// Set cookie with 30 days expiration time
document.cookie = "name=Peter; max-age=" + 30*24*60*60;
// Get name cookie value
var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)name\s*\=\s*([^;]*).*$)|^.*$/, "$1");
console.log(cookieValue); // Prints: Peter
// Update existing cookie value
document.cookie = "name=Harry; max-age=" + 30*24*60*60;
// Read the updated name cookie value
var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)name\s*\=\s*([^;]*).*$)|^.*$/, "$1");
console.log(cookieValue); // Prints: Harry
// Delete or unset the name cookie
document.cookie = "name=; max-age=0";
</script>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Install Python 3.8 in Ubuntu
Python is flexible and powerful open-sou...Django 3 CRUD Tutorial Example with MySQL and Bootstrap
Django 3 is released with full async sup...How to Check If an Object Property is Undefined in JavaScript
Use the strict equality operator (===)...Laravel 7 Guzzle Http Client Request Example
Hello Artisan, in this tutorial i am goi...Laravel 8 - Livewire Load More On Page Scroll Step by Step
in this article, you'll learn how to...