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
You can set the cookies as like
You can get the cookies as like
Hope it will helps to someone :)
EDIT:
If you want to save path of cookie for homepage alone then do this like
Thanks, vicky
A simple example of set cookie in your browser:
Simple just copy/paste and use this code for set your cookie.
I think Fresher gave us nice way, but there is a mistake:
You should add "value" near getTime(); otherwise the cookie will expire immediately :)
See the plugin:
https://github.com/carhartl/jquery-cookie
You can then do:
To delete:
Additionally, to set a timeout of a certain number of days (10 here) on the cookie:
If the expires option is omitted, then the cookie becomes a session cookie and is deleted when the browser exits.
To cover all the options:
To read back the value of the cookie:
You may wish to specify the path parameter if the cookie was created on a different path to the current one:
UPDATE (April 2015):
As stated in the comments below, the team that worked on the original plugin has removed the jQuery dependency in a new project (https://github.com/js-cookie/js-cookie) which has the same functionality and general syntax as the jQuery version. Apparently the original plugin isn't going anywhere though.
You can use a plugin available here..
https://plugins.jquery.com/cookie/
and then to write a cookie do
$.cookie("test", 1);
to access the set cookie do
$.cookie("test");
Here is my global module I use -