How can I list all cookies for the current page with Javascript?

后端 未结 8 1483
日久生厌
日久生厌 2020-12-13 01:34

Is there any way to, with help of Javascript, list all cookies associated with the current page? That is, if I don\'t know the names of the cookies but want to retrieve all

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 02:11

    function listCookies() {
        let cookies = document.cookie.split(';')
        cookies.map((cookie, n) => console.log(`${n}:`, decodeURIComponent(cookie)))
    }
    
    function findCookie(e) {
      let cookies = document.cookie.split(';')
      cookies.map((cookie, n) => cookie.includes(e) && console.log(decodeURIComponent(cookie), n))
    }
    

    This is specifically for the window you're in. Tried to keep it clean and concise.

提交回复
热议问题