0

I am using jquery cookie plugin and trying to set cookie and read cookie betwen browser sessions:

I have this piece of code to set the cookie using jquery:

<script type="text/javascript"> 
 $(document).ready(function(){
        $.cookie("example", "foo", { path: 'C:/temp', expires: 7 }); 
        alert( $.cookie("example") );
    });

</script>

I get undefined error. Any ideas?

1
  • 3
    please indicate which plugin is the one you're using... there's more than one Commented Feb 26, 2013 at 17:20

1 Answer 1

4

The problem is that you misunderstood what the path option is for.

Note: I'm assuming you're using this plugin: jquery-cookie

The following should work:

 $(document).ready(function(){
        $.cookie("example", "foo", { path: '/', expires: 7 }); 
        alert( $.cookie("example") );
    });

From the documentation:

Define the path where the cookie is valid. By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior). If you want to make it available for instance across the entire domain use path: '/'. Default: path of page where the cookie was created.

As you can see, it expects the path of the page, not a local path.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, thank you. One more question. Once I set the cookie, how do I read this from another browser session? How do I pass this to another browser session? Based on the cookie, I need to change the title of the page.
you get the cookie with: $.cookie("example"), like in your example. Do you know how HTTP cookies work? You need the current page to be in the same path (or below) that you specified for the cookie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.