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

后端 未结 8 1466
日久生厌
日久生厌 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 01:56

    No.

    The only API browsers give you for handling cookies is getting and setting them via key-value pairs. All browsers handle cookies by domain name only.

    Accessing all cookies for current domain is done via document.cookie.

    0 讨论(0)
  • 2020-12-13 02:01

    You can list cookies for current domain:

    function listCookies() {
        var theCookies = document.cookie.split(';');
        var aString = '';
        for (var i = 1 ; i <= theCookies.length; i++) {
            aString += i + ' ' + theCookies[i-1] + "\n";
        }
        return aString;
    }
    

    But you cannot list cookies for other domains for security reasons

    0 讨论(0)
  • 2020-12-13 02:05

    No there isn't. You can only read information associated with the current domain.

    0 讨论(0)
  • 2020-12-13 02:08

    Some cookies, such as referrer urls, have = in them. As a result, simply splitting on = will cause irregular results, and the previous answers here will breakdown over time (or immediately depending on your depth of use).

    This takes only the first instance of the equals sign. It returns an object with the cookie's key value pairs.

    // Returns an object of key value pairs for this page's cookies
    function getPageCookies(){
    
        // cookie is a string containing a semicolon-separated list, this split puts it into an array
        var cookieArr = document.cookie.split(";");
    
        // This object will hold all of the key value pairs
        var cookieObj = {};
    
        // Iterate the array of flat cookies to get their key value pair
        for(var i = 0; i < cookieArr.length; i++){
    
            // Remove the standardized whitespace
            var cookieSeg = cookieArr[i].trim();
    
            // Index of the split between key and value
            var firstEq = cookieSeg.indexOf("=");
    
            // Assignments
            var name = cookieSeg.substr(0,firstEq);
            var value = cookieSeg.substr(firstEq+1);
            cookieObj[name] = value;
       }
       return cookieObj;
    }
    
    0 讨论(0)
  • 2020-12-13 02:11

    Many people have already mentioned that document.cookie gets you all the cookies (except http-only ones).

    I'll just add a snippet to keep up with the times.

    document.cookie.split(';').reduce((cookies, cookie) => {
      const [ name, value ] = cookie.split('=').map(c => c.trim());
      cookies[name] = value;
      return cookies;
    }, {});
    

    The snippet will return an object with cookie names as the keys with cookie values as the values.

    Slightly different syntax:

    document.cookie.split(';').reduce((cookies, cookie) => {
      const [ name, value ] = cookie.split('=').map(c => c.trim());
      return { ...cookies, [name]: value };
    }, {});
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题