Jump to content

JavaScript Cookies


RobberBaron

Recommended Posts

In JavaScript I am creating a custom web library to speed up code references on different pages. For cookies, I already have $.setcookie, $.getcookie and $.delcookie, which if I'm being honest are modified versions of ones I've found on the internet. Now I want to add an edit cookie function, and the way I want to do that is by adding a second getcookie function that returns a table of results, and also adding a function returning the appropriate query string by cookie name. My query simply is, how would I go about making these new functions? I'm not terribly good with splitting strings, but I have an idea:

function get_t_cookie(a) {if (getcookie(a)){var b=cookiestring(a);return {'name':b.split('=')[0], 'value':b.split('=')[1], 'path':'', 'expires':'', 'domain':'','secure':''};};};

I'm looking for the cookiestring and get_t_cookie functions, or a helping hand in creating them.

Link to comment
Share on other sites

Is there a difference between "editing" a cookie and simply setting a new value/domain/date for a new cookie with the same name as the old cookie name? In other words, if you have a setCookie function, why isn't it enough? I don't get it. That's why I didn't answer when you first posted.

Link to comment
Share on other sites

Is there a difference between "editing" a cookie and simply setting a new value/domain/date for a new cookie with the same name as the old cookie name? In other words, if you have a setCookie function, why isn't it enough? I don't get it. That's why I didn't answer when you first posted.
Like you said, I'm trying to set a new value for a new cookie with the same name as the old one, but to achieve this I need to get the date, domain and secure variables from the cookie. Something like:
function editCookie(name, value) {	 setCookie(name, value, getCookie(name).expires, getCookie(name).domain);}

I've had an attempt at splitting the strings up but I think I may have misunderstood how cookies are saved. How's this:

$.add('cookietable',function(a){	b={},	c=op.cookie.split('; ');	for (d=0;d<c.length;d++){		e=(c[d].split('=')[0]=a);		if(e){			b.name=(e?a:'');			b.value=(e?$.getcookie(a):'';						//go to the next section occurence of '; ', remove the text 'expires=' and turn the result into a date and save it to the b object			b.expires=(e?new Date($.replace(c[d+1],'expires=','')):new Date());						//if the text 'path=' exists, repeat steps for expires			c[d+2].indexOf('path=')>-1&&b.path=(e?$.replace(c[d+2],'path=',''):'');						//repeat path steps for domain			c[d+3].indexOf('domain=')>-1&&b.domain=(e?$.replace(c[d+3],'domain=',''):'');						 //if the word secure exists, make a b.secure=true			b.secure=(c[d+4].indexOf('secure')>-1?true:false);		}	}	 return b;});

Then:function editCookie(name, value) {var items=cookietable(name);setCookie(name, value, items.expires, items.path, items.domain, items.secure);return getCookie(name);}

Link to comment
Share on other sites

As far as I know, JavaScript cannot read any cookie information except name-value pairs.You can get access to the cookie header that the browser sends to your server in a server-side environment like PHP. Then you could embed that information in your JavaScript script, maybe as an array, before you send the document to the browser.(I believe it is technically possible to retrieve the header by using an AJAX object, but that sounds like more trouble than it's worth.)

Link to comment
Share on other sites

As far as I know, JavaScript cannot read any cookie information except name-value pairs.You can get access to the cookie header that the browser sends to your server in a server-side environment like PHP. Then you could embed that information in your JavaScript script, maybe as an array, before you send the document to the browser.(I believe it is technically possible to retrieve the header by using an AJAX object, but that sounds like more trouble than it's worth.)
I think I might make a separate table for cookies where I can save the information when setcookie() is performed, considering in the system I'm using data is posted to each page in order to check if the user has JavaScript enabled. Then again, that does rather defeat the purpose of cookies.Thanks for the suggestions, if I ever get a spare minute I may try to set up some AJAX PHP request.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...