JQuery working with cookie method example tutorial

  • 2020-03-30 04:22:29
  • OfStack

This article illustrates how jQuery operates on cookies. Share with you for your reference. Specific methods are as follows:

Let's start with the aip for jq.cookie

Write a cookie

$.cookie("this-cookie","this-value",{
    expires:10,//Valid date
    path:"/",//Cookie path
    domanin:    //The domain name of cookie is
    secure:true //True, the transmission of cookies requires a security protocol, otherwise
});

Read the cookie

$.cookie("this-cookie")

Delete the cookie

$.cookie("this-cookie",null)

Isn't that easy? That's how you can do cookies. So let's do a demo example

$(function(){
    $("ul li").click(function(){
 $("#"+this.id).addClass("cur").siblings().removeClass("cur"); //Toggle the selected style
 $("#colortable").attr("href",this.id+".css");//Replace the corresponding style sheet
with each switch  $.cookie("cookie",//Write a cookie < br / >   this.id,//Business
that requires cookie writing   {
  "path":"/", //The default property of the cookie is
  "expires":10 //Valid days
 })
    });
    var cookie=$.cookie("cookie"); //Reading cookies < br / >     if(cookie){
     $("#"+cookie).addClass("cur").siblings().removeClass("cur");
     $("#colortable").attr("href",cookie+".css");
     $.cookie("cookie",cookie,{
  "path":"/",
  "expires":10
     })
 }
})

HTML page:

<li id="colour_1"> red </li>
<li id="colour_2"> black </li>

A simple skin peeler is available

If you open it with Google browser, remember to do it on the server side.

The things to note about the above demo are:

The box that was clicked. The class or id is the same as the corresponding stylesheet name.

And that completes the pull.

The sorted code is as follows:

$(function(){
    $("ul li").click(function(){
 Mycookie(this.id)
    });
    var cookie=$.cookie("cookie"); //Reading cookies < br / >     if(cookie){
 Mycookie(cookie);
    }
})
function Mycookie(thiscookie){
    $("#"+thiscookie).addClass("cur").siblings().removeClass("cur");
    $("#colortable").attr("href",thiscookie+".css");
    $.cookie("cookie",thiscookie,{
 "path":"/",
 "expires":10
    })
}

For more information on jquery cookie operation, please visit the special topic of this website: (link: #).


Related articles: