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 ();
}