Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Cookies using JavaScript in a local html file

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.

like image 455
Oscar Mederos Avatar asked Jun 03 '11 20:06

Oscar Mederos


People also ask

Can cookies be set by JavaScript?

JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookies that apply to the current web page.

Where are cookies stored HTML?

If you're running the 'site' on a local webserver then it should be stored in your browser under 'localhost'. If however you're just opening a static HTML file Chrome will not store the cookie. In Chrome you can use the Chrome Developer Tools and look under the 'Resources' tab.


2 Answers

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.

like image 174
lonesomeday Avatar answered Sep 18 '22 21:09

lonesomeday


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.

like image 41
Alex Wayne Avatar answered Sep 19 '22 21:09

Alex Wayne