HTML problem with cookies...

Ichinisan

Lifer
Oct 9, 2002
28,298
1,236
136
I'm experimenting with cookies and I want to store the content of a textarea object. It's multi-line. When I store and retrieve the cookie, I get only the first line from the original textarea.

createCookie:

Code:
function createCookie(cookiename,value,days){
	if(days){
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires="; expires="+date.toGMTString();
	}else{var expires="";}
	document.cookie=cookiename+"="+value+expires+"; path=/";
}

readCookie:

Code:
function readCookie(cookiename){
	var nameEQ=cookiename+"=";
	var ca=document.cookie.split(';');
	for(var i=0;i<ca.length;i++){
		var c=ca[i];
		while(c.charAt(0)==' ') c=c.substring(1,c.length);
		if(c.indexOf(nameEQ)==0){return c.substring(nameEQ.length,c.length);}
	}
	return "";
}

Can I make it so createCookie preserves line breaks in the value?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Use escape codes for hard returns, for example:
- change any "~" to "~~"
- change any \r\n or \r or \n to "~"

Be careful about limits of cookies, different browsers might truncate any one cookie at some magic number like 500 characters or 1020 characters.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Not sure what you are trying to do, but remember you can not trust any of the data in the cookie when you get it from the client.

Just because you tell me what data to store in a cookie doesn't mean I will actually do it and then return the same data.
 

Ichinisan

Lifer
Oct 9, 2002
28,298
1,236
136
It's a form I will use for myself and I can bring up any of my last 5 submissions to make minor changes and re-submit.