JavaScript stored locally

  • About
    Cookie some of the data is stored in a text file on your computer, the information is used to store user web pages
    Cookie data is in the form of key-value pairs, each key has expired time. If you do not set the time, the browser is closed, cookie will disappear
    Cookie memory size is limited, generally under each domain is about 4K, each domain would be able to store 50 key-value pairs
  • Basic operations
    can be created on the cookie by accessing document.cookie, modify and access.
    By default, cookie deleted when the browser is closed, you can also add a cookie keys for an expiration time
    if you set a new cookie, a key already exists, this key the corresponding value is updated, otherwise they exist at the same time a cookie
<script>
    // 设置cookie
    document.cookie = "username=orochiz"
    document.cookie = "age=20"

    // 读取cookie
    var msg = document.cookie
    console.log(msg) // username=orochiz; age=20

    // 添加过期时间(单位:天)
    var d = new Date() // 当前时间 2019-9-25
    var days = 3       // 3天
    d.setDate(d.getDate() + days)
    document.cookie = "username=orochiz;"+"expires="+d

    // 删除cookie (给某个键值对设置过期的时间)
    d.setDate(d.getDate() - 1)
    console.log(document.cookie)
</script>

Guess you like

Origin www.cnblogs.com/OrochiZ-/p/11585128.html