About the initial knowledge and application of cookies (JS and JQ) _ Basics

Source: Internet
Author: User
Tags set cookie time limit

What is a cookie?

---------------------------------------------------

A cookie is a mechanism provided by the browser that provides the cookie properties of the document object to JavaScript. It can be controlled by JavaScript, not by the nature of JavaScript itself. A cookie is a file that is stored on a user's hard disk, which usually corresponds to a domain name, which is available when the browser accesses the domain name again. As a result, cookies can span multiple pages under one domain name, but cannot be used across multiple domain names.

Cookie Use Occasion

---------------------------------------------------

(1) Save the user login status. For example, a user ID is stored in a cookie so that the next time a user accesses the page, there is no need to log on again, and many forums and communities now offer such functionality. Cookies can also set the expiration time, and the cookie will automatically disappear when the time limit is exceeded. As a result, the system can often prompt the user to remain logged in: The common options are one months, three months, a year, and so on.

(2) Tracking user behavior. For example, a weather forecast site that can display local weather conditions according to the user's chosen area. If you need to choose the location every time is cumbersome, when the use of cookies will appear very user-friendly, the system can remember the last visit to the region, the next time you open the page, it will automatically show the last user area of the weather. Because everything is done in the background, such a page is as easy to use as it is customized for a particular user.

(3) custom page. If the site provides the ability to change the skin or replace the layout, you can use cookies to record the user's options, such as background color, resolution, and so on. You can still save the interface style of the last visit when the user accesses the next time.

Use of Cookies

---------------------------------------------------

JS Way:

function Setcookie (sname, svalue, Oexpires, spath, Sdomain, bsecure) {//js Set cookie 
        var SCookie = sname + ' = ' + encode Uricomponent (svalue); 
         if (oexpires) { 
           var date = new Date (); 
           Date.settime (Date.gettime () + oexpires * * * 1000); 
           SCookie + = '; Expires= ' + date.toutcstring (); 
        } 
        if (spath) { 
           SCookie + = '; path= ' + spath; 
        if (sdomain) { 
           SCookie = = '; domain= ' + Sdomain; 
        } 
         if (bsecure) { 
           SCookie = = '; secure '; 
        } 
         Document.cookie = SCookie; 
 }
function GetCookie (name) {//Get cookie
    var Strcookie=document.cookie; 
    var arrcookie=strcookie.split (";"); 
    for (Var i=0;i<arrcookie.length;i++) { 
    var arr=arrcookie[i].split ("="); 
      if (arr[0]==name) {return
        decodeuricomponent (arr[1]); 
      } 
    Return ""; 
  }
function Delcookie (name) {///delete cookies
//The functions check if the cookie is set, and if set, the expiration time is adjusted to the past time;
The rest is left to the operating system at the appropriate time to clean up the cookie
if (GetCookie (name))
  {
      Document.cookie = name + "= +"; Expires=thu, 01-jan-70 00:00:01 GMT ";
  }
}

JQ Plug-in mode:

JQ official website http://plugins.jquery.com/Search Cookie plugin, the size of a few k, very convenient to use:

<script type= "Text/javascript" src= "Js/jquery.js" ></script>
<script type= "Text/javascript" src= "Js/jquery.cookie.js" ></script>

After the introduction of the above library files, the following methods are used:

<script>
    $.cookie (' The_cookie ')//Read Cookie value 
    $.cookie (' The_cookie ', ' the_value ');//Set the value 
    of the cookie $.cookie (' The_cookie ', ' The_value ', {expires:7, path: '/', Domain: ' jquery.com ', secure:true});/create a new cookie including the expiration path domain name, etc. 
    $.cookie (' The_cookie ', ' the_value ');//New Cookie 
    $.cookie (' The_cookie ', null);//delete a cookie 
 </ Script>

This article on the first knowledge of cookies and the use of (JS and JQ) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.