Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Cookie path

I use the jQuery cookie plugin for storing cookies, with the following code I can save a Cookie for 7 days, but it only saves it for the page its created on. I want the cookie to be available for the whole website.

$.cookie('basket',basket,{ expires: 7 }); 

I tried to set a path, but that didn't seem to work

$.cookie('basket',basket,{ expires: 7, path:'/' }); 

full code: this works fine, but it only saves the cookie for the current page

function add_to_basket(id,title){ if($.cookie('basket')){     basket=$.cookie('basket');      var basket_array = basket.split(',');      var index = jQuery.inArray(id,basket_array);     if(index > -1){         return false;     }else{         basket+=','+id;         $.cookie('basket',basket,{ expires: 7 });     } }else{      basket=id;     console.log(basket);     $.cookie('basket',basket,{ expires: 7 });  } 
like image 759
waterschaats Avatar asked Feb 17 '12 10:02

waterschaats


People also ask

What is jQuery cookie js?

A simple, lightweight jQuery plugin for reading, writing and deleting cookies.

How to store data in cookies in jQuery?

When the Add Cookie Button is clicked, the respective jQuery event handler is executed which saves the value of the Name TextBox to browser Cookie using the $. cookie function. The $. cookie function accepts two parameters, first the Name (Key) of the Cookie and second the Value to be stored in the Cookie.

How check cookie is set or not in jQuery?

If this is the case, then you can actually check if a user has enabled cookies or not using the following straightforward jQuery snippet: $(document). ready(function() { var dt = new Date(); dt. setSeconds(dt.


2 Answers

I just had the same problem. I fixed it by always specifying the path when writing the cookie.

$.cookie('basket', value, { path: '/' }) 

This is an issue with the jquery cookie plugin. It will default to the path of the current page.

like image 57
Tim Santeford Avatar answered Oct 17 '22 16:10

Tim Santeford


In the plugin file change:

config.defaults = {};

to

config.defaults = {path:'/'};

from https://github.com/carhartl/jquery-cookie/issues/2#issuecomment-790288

like image 36
bitlove Avatar answered Oct 17 '22 17:10

bitlove