Results 1 to 3 of 3

Thread: Adapt remember textarea code with onLoad handler

  1. #1
    Join Date
    Jun 2006
    Location
    Carmona, Seville, Spain.
    Posts
    66
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Adapt remember textarea code with onLoad handler

    Got this code from the Internet. It uses cookies to remember the user info in textarea and previous typed data is retrieved from cookie using onFocus handler. How can I adapt it so that the data is shown on page reload? Simply substituting the event handler in the code doesn't seem to work . (For IE use only).

    <html>

    <head>

    <title>A Form that Remembers</title>

    </head>

    <body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
    <script language="JavaScript">
    <!--
    // Use this function to retrieve a cookie.
    function getCookie(name){
    var cname = name + "=";
    var dc = document.cookie;
    if (dc.length > 0) {
    begin = dc.indexOf(cname);
    if (begin != -1) {
    begin += cname.length;
    end = dc.indexOf(";", begin);
    if (end == -1) end = dc.length;
    return unescape(dc.substring(begin, end));
    }
    }
    return null;
    }

    // Use this function to save a cookie.
    function setCookie(name, value, expires) {
    document.cookie = name + "=" + escape(value) + "; path=/" +
    ((expires == null) ? "" : "; expires=" + expires.toGMTString());
    }

    // Use this function to delete a cookie.
    function delCookie(name) {
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
    }

    // Function to retrieve form element's value.
    function getValue(element) {
    var value = getCookie(element.name);
    if (value != null) element.value = value;
    }

    // Function to save form element's value.
    function setValue(element) {
    setCookie(element.name, element.value, exp);
    }

    var exp = new Date();
    exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 31));
    //-->
    </script>
    <center><h1>A Form that Remembers</h1></center>
    <p>Type whatever you like in the boxes below, click outside the box, reload the page and try to type the details again. This script uses cookies, so if you have them disabled it will of course not work.</p>
    <form>

    <p><textarea name="textarea1" rows="10" onFocus="getValue(this)" onBlur="setValue(this)" cols="60"></textarea></p>
    <p><textarea name="textarea2" rows="10" onFocus="getValue(this)" onBlur="setValue(this)" cols="60"></textarea></p>
    </form>
    <p>&nbsp;</p>
    </body>

    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

  3. #3
    Join Date
    Jun 2006
    Location
    Carmona, Seville, Spain.
    Posts
    66
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    Yes, I saw that but as the page states, it ONLY works with <input type = "text"> values. I need it to work with <textarea>. The code I inserted above does. Neither is this a form with a submit button but just text areas that users fill out with study notes etc. but may not finish at one session so they'll need to come back - hence I need a simple onLoad handler to recall the previous written notes. Can you help out?

    Which brings up another point. I read somewhere that cookies can't store large amounts of text therefore textareas are impracticle for "remember values" unless it's just an email or name etc. Is that true?

Bookmarks

Posting Permissions

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