Easy-to-understand and easy-to-use cookies are the easiest way to use cookies.

Source: Internet
Author: User

Easy-to-understand and easy-to-use cookies are the easiest way to use cookies.

Today, I suddenly saw my cookie fail and shouted, how can I do this?

I am crazy about searching online.

 

1. Introduce jQuery and jQuery. Cookie. js plug-ins.

    <script src="jquery.js"></script>    <script src="jquery.cookie.js"></script>

 

 

<Html xmlns = "http://www.w3.org/1999/xhtml"> <Script src = "jquery. cookie. js "> </script> <script type =" text/javascript "> $ (function () {$ (" # username "). val ($. cookie ("username"); if ($. cookie ("like") = "Andy Lau") {$ (": radio [value = 'andy Lau ']"). attr ("checked", 'checked')} else {$ (": radio [value = 'zhang Xueyou ']"). attr ("checked", 'checked')} $ (": button "). click (function () {$. cookie ("username", $ ("# username "). val (), {path: "/", expires: 7}) $. cookie ("like", $ (": radio [checked]"). val (), {path: "/", expiress: 7 })})}) </script> 

 

 

 

                $.cookie("like", $(":radio[checked]").val(), {                    path: "/", expiress: 7                })

Create a session cookie:

$. Cookie ('cookiename', 'cookievalue ');

Note: When no cookie time is specified, the created cookie is valid until the user's browser is disabled by default. Therefore, it is called a session cookie.

Create a persistent cookie:

$. Cookie ('cookiename', 'cookievalue ', {expires: 7 });

Note: when the time is specified, it is called a persistent cookie and the validity period is day.

Create a persistent cookie with a valid path:

$. Cookie ('cookiename', 'cookievalue ', {expires: 7, path :'/'});

Note: If you do not set a valid path, you can only read the cookie on the current page of the cookie setting by default. The cookie Path is used to set the top-level directory that can read the cookie.

Create a persistent cookie with valid path and Domain Name:

$. Cookie ('cookiename', 'cookievalue ', {expires: 7, path:'/', domain: 'chuhoo. com ', secure: false, raw: false });

Note: domain: the domain Name of the webpage where the cookie is created; secure: The default value is false. If it is true, the cookie transmission protocol must be https; raw: The default value is false, this function is automatically encoded and decoded during reading and writing (encodeURIComponent encoding and decodeURIComponent decoding). disable this function and set it to true.

Get cookie:

$. Cookie ('cookiename'); // if it exists, cookieValue is returned; otherwise, null is returned.

Delete cookie:

$. Cookie ('cookiename', null );

Note: To delete a cookie with a valid path, as follows: $. cookie ('cookiename', null, {path :'/'});

 

 

That's all. If something is wrong, please give me some advice.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.