如何使用Javascript列出当前页面的所有Cookie? [英] How can I list all cookies for the current page with Javascript?

查看:187
本文介绍了如何使用Javascript列出当前页面的所有Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法,在Javascript的帮助下,列出与当前页面相关联的所有cookie?

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 the information they contain.

推荐答案

如果我不知道cookie的名称,但想要检索它们包含的所有信息。可以列出当前域的cookie:

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;
}

但由于安全原因,您无法列出其他网域的Cookie

But you cannot list cookies for other domains for security reasons

这篇关于如何使用Javascript列出当前页面的所有Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆