|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Hi,
I've been working on this for ages and l can't seem to get it to work. This function is meant to take in 'product' and if it already exists in the cookie it should remove it completely (This involves taking 'product' out of the 'thisCookie' array and then moving all the array postions -1 so there is no blank array position. e.g array = 1,2,3,4 newarray = 1,3,4 not 1,,3,4) (see first bold text). Thanks. Code:
function remove(product) {
cookie_name = "cost";
var today = new Date();
var expired = new Date(today.getTime() - 24 * 60 * 60 * 1000); // new date - 1 day, sets the expire date of the cookie to yesterday
thisCookie = document.cookie.split("::");
for(j=0; j<thisCookie.length-1; j++){
thisCookies = thisCookie[j].split("=");
for (i=1; i<thisCookies.length; i++){
pieces = thisCookies[i].split("/");
if(product == pieces[1]){
thisCookie[i] = "";
}
}
}
if(thisCookie[1] == ""){
document.cookie=cookie_name + "=null; expires=" + expired.toGMTString(); // Sets the cookie to yesterdays date
}else{
var ret = "";
for(i=0;i<thisCookie.length-1;i++){
if(ret == "") ret = thisCookie[i];
else ret += "::" + thisCookie[i];
}
MyCookie = ret+"::";
document.cookie = MyCookie;
}
}
|
|
#2
|
||||
|
||||
|
Might be easier just to write a new array and not add the given product to it if it's being removed (rather than writing a blank if it's being removed. Then you have only to join the array elements using your "::" delimiter. Some code to get you started:
Code:
thisCookie=document.cookie.split("::");
newCookie=new Array();
var count=0;
for(var i=0; i<thisCookie.length-1; i++){
if(product != thisCookie[i]){
newCookie[count]=product;
count++;
}
}
MyCookie=newCookie.join("::");
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
|
#3
|
|||
|
|||
|
Thanks yet again dhouston!!
Is there anything you don't know how to do in Javascript??? Anyway, here's the code that l used to get it to work: Code:
function remove(product) {
cookie_name = "cost";
var today = new Date();
var expired = new Date(today.getTime() - 24 * 60 * 60 * 1000); // new date -1 day, sets the expire date of the cookie to yesterday
thisCookie = document.cookie.split("::");
newCookie=new Array();
var count=0;
for(j=0; j<thisCookie.length-1; j++){
thisCookies = thisCookie[j].split("=");
for(i=0; i<thisCookies.length-1; i++){
pieces = thisCookies[1].split("/");
if(product != pieces[1]){
newCookie[count]=thisCookie[j];
count++;
}
}
}
MyCookie=newCookie.join("::");
// 'if' the cookie is empty then this deletes it
if (MyCookie == ""){
document.cookie=cookie_name + "=null; expires=" + expired.toGMTString(); // Sets the cookie to yesterdays date so it will be deleted
}
else{document.cookie = MyCookie + "::";
}
}
|
|
#4
|
||||
|
||||
|
Glad I could help. Heh, there's lots I don't know how to do in js, but I'm pretty handy with most of the basics.
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Removing a string from a array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|