Overwriting Javascript cookie with data from another cookie -


this seems simple question... trying overwrite data cookie a data stored in cookie b without changing either of names of cookies. cookie b contains particular user's defaults while cookie a stores users current selections.

i have looked online , taken few shots in dark but, have no leads! i've tried overwrites entire cookie , want data in cookie a changed, no changes names of cookies or data of other cookies

is possible? if how can accomplish task?

thank in advance!

here's code. it's commented enough answer question:

//first, "b" cookie's value var cooks=document.cookie; //just "b" part of cookie string var bstartindex=cooks.indexof('b=')+2; //get start of b part of string var bendindex=cooks.indexof(';',bstartindex); //and end of //and extract actual variable value var b=cooks.slice(bstartindex,bendindex!==-1?bendindex:null); //in case b last var, there no ; after it, bendindex -1 //now, set document.cookie='a='+b; //i don't think overwrite other cookies 

i haven't tested it, should work. tell me if doesn't , i'll try fix it

there libraries out there make lot easier, if you're planning use in production


Comments