Javascript Cookie Practices

Source: Internet
Author: User
Tags set time

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

Related Article

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.