...

View Full Version : Store HTML in cookie



shaoen01
01-18-2006, 02:11 PM
I tried storing html information such as:


<p>Test123</p>


Tried saving the code above in a cookie using javascript but cant seem to work. I had this error:
A potentially dangerous Request.Cookies value was detected from the client (desc="<STRONG>Test </STRON...").

Is there any other way i can store such html information and use it in another web page? Thanks

mark87
01-18-2006, 02:15 PM
Why don't you just add the formatting when you retrieve the cookie value?

shaoen01
01-18-2006, 02:30 PM
I do not quite understand you but from my understanding you are saying that i should do the adding of <p> tags to the value after i retrieve the "Test" value? When user do the formatting, they may go to another page and i would like the cookie to store the formatting and the value, both is equally important. And when they come back, the formatting they have made earlier is still there.


Why don't you just add the formatting when you retrieve the cookie value?

vwphillips
01-18-2006, 07:26 PM
put the <p>text<p> in an element with a unique id

then make cookie value the innerHTML of unique id

then on get cookie make the innerHTML of the element the value of the cookie

untested

cookie limits at 4K

GJay
01-18-2006, 07:47 PM
you could replace < with ~@ and > with !& (or some other combination of characters that are unlikely to appear together) and then un-replace when reading.

If you state what youre actually trying to achieve though, I'd put money on there being a better way :)

mark87
01-18-2006, 08:27 PM
Hmm though I nearly had it by escaping and unescaping, but it only works with things like '<><>', as soon as you add something like <strong> it kind of freezes...


<input type="text" id="textname" />
<input type="button" value="Set Cookie" onclick="setCookie('username',escape(document.getElementById('textname').value),365)" />
<input type="button" value="Document.Write Cookie" onclick="document.write(unescape(getCookie('username')))" />

shaoen01
01-19-2006, 05:10 AM
Could you give me an example on how to use it? Thanks


you could replace < with ~@ and > with !& (or some other combination of characters that are unlikely to appear together) and then un-replace when reading.

If you state what youre actually trying to achieve though, I'd put money on there being a better way :)

shaoen01
01-19-2006, 05:11 AM
Im not sure i quite get you, could you also give me an example on how to do it? Sorry guys, im kind of a newbie to javascript. Thanks


put the <p>text<p> in an element with a unique id

then make cookie value the innerHTML of unique id

then on get cookie make the innerHTML of the element the value of the cookie

untested

cookie limits at 4K

GJay
01-19-2006, 07:58 AM
My js is a little rusty, but something like:


html='<p>this is some text</p>';
temphtml=html.replace('<','@!;');
newhtml=temphtml.replace('>','~%');

//this is the bit I'm not sure of the syntax for...
setcookie('mycookie',newhtml);

...some where else

nothtml=getcookie('mycookie');
nearlyhtml=nothtml.replace('~%','<');
hml=nearlyhtml.replace('@!','>');
alert(html);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum