Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    63
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Store HTML in cookie

    I tried storing html information such as:
    Code:
    <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

  • #2
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why don't you just add the formatting when you retrieve the cookie value?

  • #3
    New Coder
    Join Date
    Aug 2005
    Posts
    63
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

    Quote Originally Posted by mark87
    Why don't you just add the formatting when you retrieve the cookie value?

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,517
    Thanks
    3
    Thanked 506 Times in 493 Posts
    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

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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

  • #6
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

    Code:
    <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')))" />

  • #7
    New Coder
    Join Date
    Aug 2005
    Posts
    63
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Could you give me an example on how to use it? Thanks

    Quote Originally Posted by GJay
    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

  • #8
    New Coder
    Join Date
    Aug 2005
    Posts
    63
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

    Quote Originally Posted by vwphillips
    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

  • #9
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    My js is a little rusty, but something like:
    Code:
    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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •