jquery.cookie.js use finishing (a)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u011127019/article/details/89205518

I. Introduction

Git source address:

https://github.com/carhartl/jquery-cookie

 

Cookie common attributes

A Cookie contains the following information:

1) Cookie name , Cookie name must use only the characters used in a URL, usually with letters and numbers, can not contain special characters, special characters if you want to transcode. Js The cookie can be used when operation of escape () for the name of the transcoding.

2) Cookie value , Cookie value name Similarly Cookie may be transcoded and encrypted.

3 ) the Expires , expiration date, a time in GMT format, when after this date, the browser will delete the Cookie, when not set this time, Cookie disappear after the browser is closed.

4) Path , a path, a path in the following pages can access the Cookie, usually set to "/" to show all the pages in the same site can access the Cookie.

. 5) the Domain , subdomains can access specified in the subdomain Cookie, for example, to make accessible at Cookie a.test.com, but can not access at b.test.com, the domain may be set to a .test.com.

6) Secure , security, specify whether the Cookie can only be accessed via the https protocol, the general Cookie using the HTTP protocol can access, if you set the Secure (no value), only when using the https protocol to connect cookie can be accessed page .

7) HttpOnly , if you set the "HttpOnly" attribute in the Cookie, then through the program (JS script, Applet, etc.) will not be able to read the Cookie information.

 

Second, common operations

1. Set the value of the cookie

$.cookie("name",null);

2. Create a new cookie, including expiration date, path, domain name, etc.

$.cooke("the_name","the_value",{expires:7,path:"/",domain:"query.com",secure:true});

3. Remove the cookie

$.cookie('the_name',null);

The default answer mode 4.Expire for Session

​​​​​​​$(document.body).dblclick(function () {
    $.cookie('_token_', '测试'); //Expire的默认为Session回话模式,特别说明此模式下的Cookie需要等当前浏览器所有的窗口都关闭后才会过期

});

---- Use Case

 $(function () {
          if ($.cookie("name") != null && $.cookie("pwd") != null)
          {
              $("#txtName").val($.cookie("name"));
              $("#txtPwd").val($.cookie("pwd"));
              $("#remember").attr("checked","checked");
          }
          $("#submit").click(function () {
              var name = $("#txtName").val();
          var pwd = $("#txtPwd").val();
          var checked = $("#remember").attr("checked");
              if (checked == "checked") {
                  $.cookie("name", name, {expires:2});
                  $.cookie("pwd", pwd, {expires:2});
                  alert('存入cookie');
              }
              else {
                  $.cookie("name", null);
                  $.cookie("pwd",null);
              }
          });
      });

Html part:

<body>
    用户名:<input  type="text" id="txtName"/><br />
    密码:<input  type="text" id="txtPwd"/>
    <br /><br />
    <input type="checkbox"  name="remember" id="remember" /><label for="remember" >记住密码</label>
    <input  type="button" id="submit" value="submit"/>
</body>

 

 

More:

cookie organize cross-border visits

 JQuery.cookie.js operation client cookie

Basics of Cookie

Guess you like

Origin blog.csdn.net/u011127019/article/details/89205518