This time it gives a reasonable cookie operation code:
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 use of cookies within the specified time, otherwise it will never expire document.cookie=k+ "=" +v+ "; expires=" + (E! = "? Date.togmtstring ():" Gmt_ String ") +";p ath=/;d omain= "+ (d| |"); }, del:function (k) { var date=new date (); Sets date to the 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, the next time it is opened or hidden, when the content is displayed, the next time it is opened or displayed.
<div class= "tab" >
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 = ' contraction ';} function Hide () {TabCon.style.display = ' none '; Btn.setattribute (' Data-isclose ', ' close '); btn.innerhtml = ' unfold ';} var flag = cookie.get (' flag '); if (flag = = ' Hide ') {hide ();}
Javascript Cookie Practices