在本地 html 文件中使用 JavaScript 设置 Cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6232331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Cookies using JavaScript in a local html file
提问by Oscar Mederos
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?
如果我在page1.html
使用 JavaScript 时设置了一些 cookie ,那么该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.
编辑:
让我更好地解释一下。我正在处理本地文件。page1.html
正在通过/home/user/.../folder1/folder2/page1.html
而不是通过使用 HTTP 服务器的客户端机器访问。
Just to clarify:
It seems that some browsers (like Chrome) do not store cookies when using file:///
, but both Firefox and Internet Explorer do.
澄清
一下:似乎某些浏览器(如 Chrome)在使用 时不存储 cookie file:///
,但 Firefox 和 Internet Explorer 都可以。
采纳答案by lonesomeday
From the MDC page for document.cookie
:
If not specified, [the
path
argument] defaults to the current path of the current document location.
如果未指定,[
path
参数] 默认为当前文档位置的当前路径。
So in your case, it will be /folder1/folder2/
.
所以在你的情况下,它将是/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.
我最初没有看到您在问题标题中指定了“本地”——不确定在我写答案时是否已更新。使用file:///
协议浏览时不会设置 Cookie ,具体取决于浏览器。
回答by Alex Wayne
Browsers do not store cookies for the file://
url protocol, it will simply and silently fail to set anything at all. So if this is truly "local" and not on a domain you may have a problem.
浏览器不会为file://
url 协议存储 cookie ,它会简单而静默地根本无法设置任何内容。因此,如果这确实是“本地”而不是域上,您可能会遇到问题。
回答by Carl Sednaoui
If you're on a mac, you can close Chrome and relaunch it like so:
如果您使用的是 mac,您可以关闭 Chrome 并重新启动它,如下所示:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --enable-file-cookies
You'll then be able to set cookies on local files.
然后,您就可以在本地文件上设置 cookie。
回答by craniumonempty
set --enable-file-cookies for chrome and it should work for you. Also, there are some features that you'll have to set "accept all cookies" also to make work, but if you do, make sure you set back before going back online.
为 chrome 设置 --enable-file-cookies,它应该适合你。此外,有些功能您还必须设置“接受所有 cookie”才能工作,但如果您这样做了,请确保在重新上线之前重新设置。
回答by Yaroslav Wally
As workaround you can use Tampermonkey with access to local files ( How to include Local htm pages in a Tampermonkey script?) By that way you will use Tampermonkey's storage, and will be able to set and get your data by functions GM_getValue(data) and GM_setValue(data). I used that for my local HTML page, which i used as customizable alternative to Windows Explorer
作为解决方法,您可以使用 Tampermonkey 访问本地文件(如何在 Tampermonkey 脚本中包含本地 htm 页面?)通过这种方式,您将使用 Tampermonkey 的存储,并且将能够通过函数 GM_getValue(data) 和GM_setValue(数据)。我将它用于我的本地 HTML 页面,我将其用作 Windows 资源管理器的可自定义替代品