使用本地html文件中的JavaScript设置Cookie [英] Setting Cookies using JavaScript in a local html file
问题描述
我有以下目录树:
+ folder1
|--- folder2
|------ page1.html
|--- page2.html
如果我使用JavaScript在 page1.html
中设置了一些Cookie,该Cookie使用的路径是什么?
If I set some cookie in page1.html
using JavaScript, what is the path used for that cookie?
编辑:
让我更好地解释一下。我正在使用本地文件。 page1.html
正在通过 / home / user /.../ folder1 / folder2 / page1.html
不是通过使用HTTP Server的客户端计算机。
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.
只是为了澄清:
看起来有些浏览器Chrome)在使用 file:///
时不保存Cookie,但是Firefox和Internet Explorer都会这样做。
Just to clarify:
It seems that some browsers (like Chrome) do not store cookies when using file:///
, but both Firefox and Internet Explorer do.
推荐答案
从 document.cookie 的 MDC页面, code> :
如果未指定,[
path
argument]默认为当前文档位置的当前路径。
If not specified, [the
path
argument] defaults to the current path of the current document location.
所以在你的情况下, c> / folder1 / folder2 / 。
So in your case, it will be /folder1/folder2/
.
d在问题标题中指定本地 - 不确定这是否在我写我的答案时更新。使用 file:///
协议浏览时,不会设置Cookie,具体取决于浏览器。
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屋!