Setting Cookies using JavaScript in a local html file

对着背影说爱祢 提交于 2019-11-27 04:21:55

问题


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.


回答1:


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.




回答2:


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.




回答3:


If you're on a mac, you can close Chrome and relaunch it like so:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --enable-file-cookies

You'll then be able to set cookies on local files.




回答4:


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.



来源:https://stackoverflow.com/questions/6232331/setting-cookies-using-javascript-in-a-local-html-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!