A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to Set or Unset a Cookie with jQuery
Q:

How to Set or Unset a Cookie with jQuery

0

How to Set or Unset a Cookie with jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the JS document.cookie Property

You 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>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now