Javascript Cookie在其他目录中不可用

Javascript Cookie在其他目录中不可用,javascript,cookies,cookie-path,Javascript,Cookies,Cookie Path,根据MDN,Cookie设置为“/”路径。这一点得到了国际货币基金组织的证实 然而,我还没有发现这种情况。我使用firefox在google.com上的(不存在的)目录上运行了以下测试: > window.location = 'https://google.com/dir/file.html' Navigated to https://google.com/dir/file.html "https://google.com/dir/file.html" > document.coo

根据MDN,Cookie设置为“/”路径。这一点得到了国际货币基金组织的证实

然而,我还没有发现这种情况。我使用firefox在
google.com
上的(不存在的)目录上运行了以下测试:

> window.location = 'https://google.com/dir/file.html'
Navigated to https://google.com/dir/file.html
"https://google.com/dir/file.html"
> document.cookie
""
> document.cookie = 'key1=value1,path=/'
"key1=value1,path=/"
> document.cookie
"key1=value1,path=/"
> window.location = '/dir2/file.html'
Navigated to https://google.com/dir2/file.html
"/dir2/file.html"
> document.cookie
""

我是不是误解了什么?我做错了什么?我在Chrome上做了类似的测试,结果也一样。

找到了答案。我用逗号分隔符而不是分号分隔符设置路径。它是在我键入cookie时按字面意思编写cookie,而不是解释路径和域

编写cookie的正确方法应该是
document.cookie='key1=value1;路径=/'