Michael Falciglia
Michael Falciglia

Reputation: 1046

Jquery Cookie Path Issue

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>

Upvotes: 0

Views: 3649

Answers (2)

Jai
Jai

Reputation: 74738

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>

Upvotes: 1

Rob M.
Rob M.

Reputation: 36511

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

Upvotes: 0

Related Questions