在本地 html 文件中使用 JavaScript 设置 Cookie [英] Setting Cookies using JavaScript in a local html file
问题描述
我有以下目录树:
+ folder1|--- 文件夹2|------ page1.html|--- page2.html
如果我使用 JavaScript 在 page1.html
中设置了一些 cookie,那么该 cookie 使用的路径是什么?
让我更好地解释一下.我正在处理本地文件.page1.html
是通过 /home/user/.../folder1/folder2/page1.html
而不是通过使用 HTTP 服务器的客户端机器访问的.>
只是为了澄清:
似乎某些浏览器(如 Chrome)在使用 file:///
时不存储 cookie,但 Firefox 和 Internet Explorer 都可以.
如果未指定,[path
参数]默认为当前文档位置的当前路径.
所以在您的情况下,它将是 /folder1/folder2/
.
我最初没有看到您在问题标题中指定了本地"——不确定在我写答案时是否已更新.使用 file:///
协议浏览时不会设置 Cookie,具体取决于浏览器.
I have the following directory tree:
+ folder1
|--- folder2
|------ page1.html
|--- page2.html
If I set some cookie in page1.html
using JavaScript, what is the path used for that cookie?
Edit:
Let me explain it better. I'm working with a local file. page1.html
is being accessed through /home/user/.../folder1/folder2/page1.html
and not through a client machine using a HTTP Server.
Just to clarify:
It seems that some browsers (like Chrome) do not store cookies when using file:///
, but both Firefox and Internet Explorer do.
From the MDC page for document.cookie
:
If not specified, [the
path
argument] defaults to the current path of the current document location.
So in your case, it will be /folder1/folder2/
.
I didn't initially see that you'd specified "local" in the question title -- not sure if this was updated while I was writing my answer. Cookies are not set when browsing using the file:///
protocol, depending on the browser.
这篇关于在本地 html 文件中使用 JavaScript 设置 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!