/    Sign up×
Community /Pin to ProfileBookmark

Correction to the "Save and Restore Form Cookies" javascript functions

I found that the recently highlighted JavaScript Source: Cookies : Save and Restore Form Cookies works very nicely, after a couple of things are changed.

1)
Change var formname = frm.id;
To var formname = frm.name;

  • this occurs in both saveSelections() and loadSelections()
  • 2)
    add to the saveSelections()

    if (fieldType == “hidden”) {
    //alert(frm.elements[e].length);
    string = string + frm.elements[e].value + “|“;
    //alert(“text”);
    }

    3)

    add to the saveSelections()

    if (fieldType == “hidden”) frm.elements[e].value = fieldValue;

    It would also be useful to add the instructions on how to implement it:

    Change <BODY>
    To <BODY onunload=”saveSelections(document.forms[0])”>

    and

    Change </FORM>
    TO </FORM><SCRIPT language=JavaScript type=””>loadSelections(document.forms[0]);</SCRIPT>

    You also need to make sure you already have some functions called
    getCookie(name)
    and
    setCookie(name, value, expires, path, domain, secure)

    I used routines which I picked up somewhere (can’t remember where):

    function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + “=” + escape(value) +
    ((expires) ? “; expires=” + expires.toGMTString() : “”) +
    ((path) ? “; path=” + path : “”) +
    ((domain) ? “; domain=” + domain : “”) +
    ((secure) ? “; secure” : “”);
    }

    function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + “=”;
    var begin = dc.indexOf(“; ” + prefix);
    if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
    } else {
    begin += 2;
    }
    var end = document.cookie.indexOf(“;”, begin);
    if (end == -1) {
    end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
    }

    David IB

    to post a comment
    JavaScript

    1 Comments(s)

    Copy linkTweet thisAlerts:
    @LeeUFeb 08.2006 — Thanks, David. I'll take a look and make the necessary corrections.
    ×

    Success!

    Help @David-IB spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...