JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old August 3rd, 2004, 03:27 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation Removing a string from a array

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;
	}
}	

Reply With Quote
  #2  
Old August 3rd, 2004, 06:40 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
  #3  
Old August 3rd, 2004, 09:36 AM
Justin2k Justin2k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 27 Justin2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

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 + "::";
        }
}

Reply With Quote
  #4  
Old August 3rd, 2004, 09:50 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Removing a string from a array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway