How do I set and unset a cookie using jQuery, for example create a cookie named test
and set the value to 1
?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
Make sure not to do something like this:
Then, if the cookie doesn't exist, the debugger will return some unhelpful message like ".cookie not a function".
Always declare first, then do the split after checking for null. Like this:
There is no need to use jQuery particularly to manipulate cookies.
From QuirksMode (including escaping characters)
Take a look at
You can use the library on Mozilla website here
You'll be able to set and get cookies like this
Here is how you set the cookie with jQuery:
below code has been taken from https://www.w3schools.com/js/js_cookies.asp
now you can get the cookie with below function:
And finally this is how you check the cookie:
If you want to delete the cookie just set the expires parameter to a passed date:
I thought Vignesh Pichamani's answer was the simplest and cleanest. Just adding to his the ability to set the number of days before expiration:
EDIT: also added 'never expires' option if no day number is set
Set the cookie: