Reputation: 146
I have an issue when i am trying to delete a cookie if i have set it on a different page. Here is the scenario:
I am on my homepage and set a cookie value(by adding a product to my basket), i can delete this while i am on that page.
But when i go on a different page and try to delete the same cookie(product), the cookie is created again with the new value, it does not re-set my current cookie.
I am using this code to add/remove the cookie:
$(".add-basket").click(function(e){
e.preventDefault();
var ean = $(this).attr('ean');
var value = $(this).attr("value");
var path = $(this).attr("imgpath");
if($.cookie("mycookie")==undefined || $.cookie("mycookie")==""){
$.cookie("mycookie" , ean + "~" + value + "~" + path);
} else if($.cookie("mycookie").indexOf(ean) == -1 ){
$.cookie("mycookie",$.cookie("mycookie") + "|" + ean + "~" + value + "~" + path);
}
});
$(".remove-basket").click(function(e){
e.preventDefault();
var ean = $(this).attr('ean');
var value = $(this).attr('value');
var path = $(this).attr("imgpath");
var val = ean + "~" + value + "~" + path; //value to be removed
if ($.cookie("mycookie") !== undefined) {
var cookie_val = $.cookie("mycookie");
if (cookie_val.indexOf(val) !== -1) {
//check value present in cookie
var arr = cookie_val.split('|'); //remove spaces and split with |
var index = arr.indexOf(val);//get index of value to be deleted
arr.splice(index, 1); //remove value from array
$.cookie("mycookie", arr.join('|')); //convert array to sting using join and set value to cookie
}
}
Am i missing something?
Please help
Upvotes: 1
Views: 4059
Reputation: 1
You dont even need to clear the cookie, just set path same , for instance you can set path as "/" which cookie has scope throughout the application/site. But, if you dont specify path, it will automatically take the path of the current folder where your code file is placed from which you are setting cookie.
Upvotes: 0
Reputation: 17607
You need to set the cookie valid domain, try set cookie using:
$.cookie("mycookie", 'value', {path: '/'});
this will make the cookie valid on the whole site, if this doesn't work, try to clear your old cookie.
Here is example of using JSON:
// sample data
var data = [
{ ean: 1, value: 2, path: 3},
{ ean: 1, value: 2, path: 3}
];
// insert data
data.push({ ean:2, value: 3, path: 4});
// save to cookie
$.cookie('mycookie', JSON.stringify(data), {path: '/'});
// load from cookie
data = JSON.parse( $.cookie('mycookie') );
So, change your handler to:
$(".add-basket").click(function(e){
e.preventDefault();
var ean = 1;
var value = 1;
var path = 1;
var cookie = JSON.parse( $.cookie("mycookie") || '[]' );
cookie.push({
ean: ean,
value: value,
path: path
});
$.cookie("mycookie", JSON.stringify(cookie), {path: '/'});
});
$(".remove-basket").click(function(e){
e.preventDefault();
var ean = $(this).attr('ean');
var value = $(this).attr('value');
var path = $(this).attr("imgpath");
var cookie = JSON.parse( $.cookie("mycookie") || '[]' );
$.each(cookie, function (index, row) {
if (row.value == value) {
cookie.splice(index, 1);
}
});
$.cookie("mycookie", JSON.stringify(cookie), {path: '/'});
}
Upvotes: 2
Reputation: 3765
Maybe your cookie is only valid on one page? You have to set the pathname for your cookie.
$.cookie("mycookie" , ean + "~" + value + "~" + path, { path: '/' } ); // path:'/' is setting a cookie which is valid for all pages on your domain
Upvotes: 0
Reputation: 198324
It seems you have a problem with cookie Path
being set. From jQuery Cookie plugin documentation:
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.
Upvotes: 1