41

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.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
  • Cookie it independent of the page where it was created, the "path" consists only of the website domain. (Assuming you mean the cookie name as it appears in browser's cookie list) – Shadow The GPT Wizard Jun 03 '11 at 20:33
  • See my edit. I don't know why I got a downvote `:(` – Oscar Mederos Jun 03 '11 at 20:34
  • Sorry, thought you didn't check some basic information - disregard. – Shadow The GPT Wizard Jun 03 '11 at 20:36
  • Anybody know whether this "feature" has been added in Firefox 57? I'm trying to debug a problem--my local script used to work in FF until 57, and now it seems to lose the cookie. That FF has gone over to a no-local-cookies default is one of my hypotheses. However, it's nearly impossible to web search; all you get is noise about how to enable/disable cookies, or about add-on cookie managers. This question is the closest I've come. – Mars Nov 20 '17 at 17:42
  • Seems no longer available https://bugs.chromium.org/p/chromium/issues/detail?id=470482 – Ishan Fernando Mar 25 '20 at 05:06

6 Answers6

23

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.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
23

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.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 2
    It seems they do. I'm setting a cookie in `page1.html`, then close the browser, open the page again (locally, of course) and then I ask for that cookie and it is still there :? – Oscar Mederos Jun 03 '11 at 20:36
  • 2
    @Oscar I think this is a cross-browser thing. Chrome has a slightly different interpretation of the same-origin policy compared to Firefox or Internet Explorer, particularly when it comes to the `file:///` protocol. – lonesomeday Jun 03 '11 at 20:39
  • In my case it works in Firefox 77 and does not work in Chrome 83. Both on Ubuntu 20 – Pouria Moosavi Jun 13 '20 at 14:11
12

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.

Carl Sednaoui
  • 934
  • 9
  • 15
  • 1
    +1: This worked. Used in Terminal. I had to remove the second dash on "--enable-file-cookies" flag, so it should look like: `open /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome -enable-file-cookies` – FreeAsInBeer Jun 18 '13 at 18:50
  • 1
    This flag no longer works as the feature has been removed: https://bugs.chromium.org/p/chromium/issues/detail?id=470482 – hwgn Apr 19 '23 at 09:11
5

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.

craniumonempty
  • 3,525
  • 2
  • 20
  • 18
1

For those who are still looking for a way, try setting URL parameters instead of cookies

I had downloaded some novels in HTML for offline reading and there was no way to pass previously set font size to next chapters using cookies so what I did is shown below :

  1. First add a widow event listener that adds the font size parameter whenever a link(to next ch) is clicked
  2. And then window.onload function that takes the parameter value and changes the font size

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <a href="ch-02.html" >Next Chapter</a>
    <p id="content">Hope this helps</p>
    
<script>
var fontSize = 1;
window.addEventListener("click", function(e) {
    var href = e.target.getAttribute("href");
    if(href) {
        location.href = href + "?fontSize=" + fontSize;
        e.preventDefault();
     }
})
window.onload = function(){
  var url = new URL(window.location.href);
  var value = url.searchParams.get("fontSize");
  if(value != null){
    fontSize = parseFloat(value);
    document.getElementById("content").style.fontSize = fontSize + "em";
  }
}
</script>
    
  </body>
</html>
0

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