Home > Web Front-end > JS Tutorial > First understanding and application of cookies (js and jq)_Basic knowledge

First understanding and application of cookies (js and jq)_Basic knowledge

WBOY
Release: 2016-05-16 15:06:13
Original
1816 people have browsed it

What are cookies

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

Cookie is a mechanism provided by the browser, which provides the cookie attribute of the document object to JavaScript. It can be controlled by JavaScript and is not a property of JavaScript itself. A cookie is a file stored on the user's hard drive. This file usually corresponds to a domain name. When the browser accesses the domain name again, the cookie is made available. Therefore, cookies can span multiple web pages under one domain name, but cannot be used across multiple domain names.

Cookie use cases

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

(1) Save user login status. For example, the user ID is stored in a cookie so that the user does not need to log in again when he visits the page next time. Many forums and communities now provide this function. Cookies can also set an expiration time. When the time limit expires, the cookie will automatically disappear. Therefore, the system can often prompt users to stay logged in: common options are one month, three months, one year, etc.

(2) Track user behavior. For example, a weather forecast website can display local weather conditions based on the region selected by the user. If you need to select the location every time, it will be cumbersome. When cookies are used, it will become very user-friendly. The system can remember the area visited last time. When the page is opened next time, it will automatically display the last user. Weather conditions in your area. Because everything is done in the background, such a page is as if it is customized for a certain user and is very convenient to use.

(3) Customized page. If the website provides the function of changing the skin or changing the layout, cookies can be used to record the user's options, such as background color, resolution, etc. When the user visits next time, the interface style of the last visit can still be saved.

How to use cookies

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

js method:

function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) { //js设置cookie 
        var sCookie = sName + '=' + encodeURIComponent(sValue); 
         if (oExpires) { 
           var date = new Date(); 
           date.setTime(date.getTime() + oExpires * 60 * 60 * 1000); 
           sCookie += '; expires=' + date.toUTCString(); 
        } 
        if (sPath) { 
           sCookie += '; path=' + sPath; 
        if (sDomain) { 
           sCookie += '; domain=' + sDomain; 
        } 
         if (bSecure) { 
           sCookie += '; secure'; 
        } 
         document.cookie = sCookie; 
 }
Copy after login
function getCookie(name){ //获取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 ""; 
  }
Copy after login
function delCookie(name){//删除cookie
// 该函数检查下cookie是否设置,如果设置了则将过期时间调到过去的时间;
//剩下就交给操作系统适当时间清理cookie啦
if (getCookie(name))
  {
      document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
Copy after login

jq plug-in method:

jq official websitehttp://plugins.jquery.com/ Search cookie plug-in, several kilobytes in size, very easy to use:


After introducing the above library files, the usage method is as follows:

<script>
    $.cookie('the_cookie'); //读取Cookie值 
    $.cookie('the_cookie', 'the_value'); //设置cookie的值 
    $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一个cookie 包括有效期 路径 域名等 
    $.cookie('the_cookie', 'the_value'); //新建cookie 
    $.cookie('the_cookie', null); //删除一个cookie 
 </script>
Copy after login

The above article about the first understanding and application of cookies (js and jq) is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template