Use JS read, write, delete cookie code sequel _javascript tips

Source: Internet
Author: User
Tags set time

Last article: Use JS read, write, delete cookie code sharing and detailed notes, in practice, found some problems:

1.cookie can only be debugged on Firefox on local files, IE and chrome are not valid

2.cookie is not set to never expire, it is obviously not reasonable to consider setting a time period to expire.

This is a more reasonable cookie operation code:

Copy Code code as follows:

var Cookie = {
Get:function (k) {
Return (New RegExp (["(?:;)?", K, "= ([^;] *);?"]. Join ("")). Test (Document.cookie) && regexp["$") | | "";
},
Set:function (k, V, E, D) {
var date=new date ();
var expiresdays=e;
Date.settime (Date.gettime () +expiresdays*24*3600*1000);
If there is a set time, the cookie is used within the specified time, otherwise it is never expired
document.cookie=k+ "=" +v+ "; Expires= "+ (e!= '? Date.togmtstring ():" gmt_string ") +";p ath=/;d omain= "+ (d| |");
},
Del:function (k) {
var date=new date ();
Set Date to past time
Date.settime (Date.gettime ()-10000);
document.cookie=k+ "=; Expires= "+date.togmtstring ();
}
};

The example shows: Click on the text to expand the content, click Hide again. When the content is hidden, next open or hidden, when the content is displayed, next open or display.

Copy Code code as follows:

<div class= "tab" >

<div class= "Tab-con" id= "Tabcon" >
<p> after the expansion of the content here </p>
</div>
</div>
var btn = document.getelementsbytagname (' h3 ') [0];
Btn.addeventlistener (' click ', function () {
var isclose = This.getattribute (' Data-isclose ');
if (Isclose = = ' Close ') {
Show ();
Cookie.del (' flag ');
}else{
Hide ();
Cookie.set (' flag ', ' hide ');
}
});
var Tabcon = document.getElementById (' Tabcon ');
Function Show () {
TabCon.style.display = ' block ';
Btn.setattribute (' data-isclose ', ' open ');
btn.innerhtml = ' shrink ';
}
function Hide () {
TabCon.style.display = ' None ';
Btn.setattribute (' data-isclose ', ' close ');
btn.innerhtml = ' unfold ';
}
var flag = cookie.get (' flag ');
if (flag = = ' Hide ') {
Hide ();
}

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.