0

I am using Jquery Cookie and I am trying to retrieve a cookie in a different directory that I set like this:

<script>
    $(document).ready(function () {
    $("#saveForm").click(function () {
     $.cookie('myCookie', $("#Website").val(), {
    expires: 365,
    path: '/'
         });
     });
</script>

The cookie is stored, I verified it in my browser's cookies. So I am trying to retreive it with this, but its not getting it. Is there something wrong with the path or is my code wrong?

This is the code I am using to try to retrieve it with:

<script>
    $(document).ready(function () {
    $("#Website").val($.cookie('myCookie'));
    path: '/'
    });
</script>
Michael Falciglia
  • 1,046
  • 4
  • 20
  • 36
  • I don't think it is possible to read cookie from a different path – Arun P Johny Nov 28 '13 at 06:37
  • What's that `path: '/'` doing there, outside an object literal? Anyway, you don't need to specify the path when reading a cookie, only when setting it. – Barmar Nov 28 '13 at 06:37
  • see http://stackoverflow.com/questions/1967963/how-to-access-cookie-values-on-different-paths-of-the-same-domain-using-php – Arun P Johny Nov 28 '13 at 06:38
  • if you want to allow anybody from the same domain to read the cookie just leave the path setting – Arun P Johny Nov 28 '13 at 06:38
  • You can access a cookie if the path is a parent of your current path. – Barmar Nov 28 '13 at 06:38
  • @barmar I only tried to use the path because it is only working on the pages in the directory I set the cookie in. So I tried to add the path – Michael Falciglia Nov 28 '13 at 07:13
  • But you didn't even add it as an argument to `$.cookie`, it's just sitting in your function doing nothing. – Barmar Nov 28 '13 at 07:14
  • @ArunPJohny I used the code you gave me the other day and it worked at setting it and retrieving it on pages in the same directory, but when I tried to retrieve it on a page outside of the directory it did not work, so that is why I am trying a path. – Michael Falciglia Nov 28 '13 at 07:16

2 Answers2

1

Not sure but your code has some typos:

$.cookie Reference here

<script>
    $(document).ready(function () { //<-------------no end tag of this
    $("#saveForm").click(function () {
     $.cookie('myCookie', $("#Website").val(), {
    expires: 365,
    path: '/'
         }); //<---end of $.cookie
     }); //<----end of .click
</script>

so this should be like this:

 <script>
      $(document).ready(function () { 
         $("#saveForm").click(function () {
            $.cookie('myCookie', $("#Website").val(), {
                expires: 365,
                path: '/'
            }); //<---end of $.cookie
          }); //<----end of .click
      }); //<----end of doc ready
</script>

and with reading cookies you have to do just this as you mentioned the global cookie:

<script>
      $(document).ready(function () {
         $("#Website").val($.cookie('myCookie'));
      });
</script>

So the final code should be:

 <script>
      $(document).ready(function () { 
         $("#saveForm").click(function () {
            $.cookie('myCookie', $("#Website").val(), {
                expires: 365,
                path: '/'
            }); //<---end of $.cookie
          }); //<----end of .click

         $("#Website").val($.cookie('myCookie'));

      }); //<----end of doc ready
</script>
Jai
  • 74,255
  • 12
  • 74
  • 103
  • Thanks I am going to try it now – Michael Falciglia Nov 28 '13 at 06:57
  • 1
    Thanks for the cleaning the code up. The only thing is that was not the problem. The problem I was asking for help with is when I try to retrieve the cookie from a different directory, it does not find it. I have no problems getting it when it is in the same directory – Michael Falciglia Nov 28 '13 at 07:06
0

The path is not referring to a directory that the cookie is stored in, it's referring to what url the cookie is valid and available for

Rob M.
  • 35,491
  • 6
  • 51
  • 50