如何获取Javascript中的cookie的域值? [英] How to get the domain value for a cookie in Javascript?

查看:187
本文介绍了如何获取Javascript中的cookie的域值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Javascript我想获得特定Cookie的 domain 值。

Using Javascript I'd like to get the domain value for a specific cookie.

?如果是,如何?

说明:我不是在查找cookie的。我在subdomain.domain.com,我需要删除名称是已知的,但其域值类似于.domain.com的cookie。总之:我想获得.domain.com的值。

To clarify: I'm not looking for the value of the cookie. I'm on "subdomain.domain.com" and I need to remove a cookie whose name is known but its domain value is something like ".domain.com". In short: I'd like to get the value of ".domain.com".

推荐答案

您在 document.cookie 中看到的内容。 path domain expires 的Cookie元数据不

Sorry, all you get is what you see in document.cookie. The cookie metadata like path, domain and expires are not visible to site code (neither to JavaScript nor to the server-side).

要读取被具有更多特定<$ c>的cookie遮蔽的cookie $ c> domain 或 path ,你唯一可以做的是加载一个页面,其中更多的特定的cookie超出范围,并从那里读取。

To read a cookie that is being shadowed by a cookie with a more-specific domain or path, the only thing you can do is load a page for which the more-specific cookie is out-of-scope, and read it from there.

如果,如你所说,你只需要删除一个cookie,你可以做的是尝试在每个可能的特定级别删除cookie,例如:

If, as you say, you only need to remove a cookie, what you could do is try to remove the cookie at every possible level of specificity, eg.:

    document.cookie= 'foo=;domain=sub.domain.example.com;expires=Sat, 01-Jan-2000 00:00:00 GMT';
    document.cookie= 'foo=;domain=domain.example.com;expires=Sat, 01-Jan-2000 00:00:00 GMT';
    document.cookie= 'foo=;domain=example.com;expires=Sat, 01-Jan-2000 00:00:00 GMT';

,与 path 变量类似。你可以把这个放在每个路径和域部分的嵌套循环中,分开为域, / 路径。

and similarly with the path variable. You could put this in a nested loop for each path and domain part, splitting on . for the domain and / for the path.

这篇关于如何获取Javascript中的cookie的域值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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