help please..how to unescape() the value of a cookie?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • koen colen

    help please..how to unescape() the value of a cookie?

    Hello group,

    I hope you guys can help me out, I am modifying a piece of code from Joe
    Norman,
    I did found the code here:


    My question:
    How can I save the value's of a form to a cookie, but in the textarea's of
    that form the "hard returns" must be saved with it?
    I have this code:





    <html><HEAD>
    <SCRIPT LANGUAGE="JavaS cript">
    var arrRecords = new Array();
    var arrCookie = new Array();
    var recCount = 0;
    var strRecord="";
    expireDate = new Date;
    expireDate.setD ate(expireDate. getDate()+365);

    function cookieVal(cooki eName) {
    thisCookie = document.cookie .split("; ")
    for (i = 0; i < thisCookie.leng th; i++) {
    if (cookieName == thisCookie[i].split("=")[0]) {
    return thisCookie[i].split("=")[1];
    }
    }
    return 0;
    }

    function loadCookie() {
    if(document.coo kie != "") {
    if(cookieVal("R ecords") != ""){
    arrRecords = cookieVal("Reco rds").split("," );
    }
    currentRecord() ;
    }
    }

    function setRec() {
    strRecord = "";
    for(i = 0; i < document.frm1.e lements.length; i++) {
    strRecord = strRecord + document.frm1.e lements[i].value + ":";
    }
    arrRecords[recCount] = strRecord;
    document.frm2.a dd.value = " NEW ";
    document.cookie = "Records="+arrR ecords+";expire s=" +
    expireDate.toGM TString();
    }


    function newRec() {
    switch (document.frm2. add.value) {
    case " NEW " :
    varTemp = recCount;
    for(i = 0; i < document.frm1.e lements.length; i++) {
    document.frm1.e lements[i].value = ""
    }
    recCount = arrRecords.leng th;
    document.frm2.a dd.value = "CANCEL";
    break;
    case "CANCEL" :
    recCount = varTemp;
    document.frm2.a dd.value = " NEW ";
    currentRecord() ;
    break;
    }
    }

    function countRecords() {
    document.frm2.a ctual.value = "Record " + (recCount+1)+";
    "+arrRecords.le ngth+" saved records";
    }

    function delRec() {
    arrRecords.spli ce(recCount,1);
    navigate("previ ous");
    setRec();
    }

    function currentRecord() {
    if (arrRecords.len gth != "") {
    strRecord = arrRecords[recCount];
    currRecord = strRecord.split (":");
    for(i = 0; i < document.frm1.e lements.length; i++) {
    document.frm1.e lements[i].value = currRecord[i];
    }
    }
    }

    function navigate(contro l) {
    switch (control) {
    case "first" :
    recCount = 0;
    currentRecord() ;
    document.frm2.a dd.value = " NEW ";
    break;
    case "last" :
    recCount = arrRecords.leng th - 1;
    currentRecord() ;
    document.frm2.a dd.value = " NEW ";
    break;
    case "next" :
    if (recCount < arrRecords.leng th - 1) {
    recCount = recCount + 1;
    currentRecord() ;
    document.frm2.a dd.value = " NEW ";
    }
    break;
    case "previous" :
    if (recCount > 0) {
    recCount = recCount - 1;
    currentRecord() ;
    }
    document.frm2.a dd.value = " NEW ";
    break;
    default:
    }
    }

    // Splice method Protype Function
    // Peter Belesis, Internet.com
    // http://www.dhtmlab.com/

    function pageLoad(){
    if (!Array.prototy pe.splice) {
    function array_splice(in d,cnt) {
    if (arguments.leng th == 0) return ind;
    if (typeof ind != "number") ind = 0;
    if (ind < 0) ind = Math.max(0,this .length + ind);
    if (ind > this.length) {
    if (arguments.leng th > 2) ind = this.length;
    else return [];
    }
    if (arguments.leng th < 2) cnt = this.length-ind;
    cnt = (typeof cnt == "number") ? Math.max(0,cnt) : 0;
    removeArray = this.slice(ind, ind+cnt);
    endArray = this.slice(ind+ cnt);
    this.length = ind;
    for (var i = 2; i < arguments.lengt h; i++) {
    this[this.length] = arguments[i];
    }
    for(var i = 0; i < endArray.length ; i++) {
    this[this.length] = endArray[i];
    }
    return removeArray;
    }
    Array.prototype .splice = array_splice;
    }
    recCount = 0;
    loadCookie();
    countRecords();
    }

    </script>
    </HEAD>

    <body bgcolor="black" text="red" onLoad="pageLoa d()"><center>

    <center>
    <form name="frm1">
    <table width="391" border="1" align="center" resize="none">
    <!--DWLayoutTable-->
    <tr>
    <td width="381" height="310" valign="top"><t extarea name="textarea"
    cols="57" rows="5">1) I want to save this text
    2) in a cookie,
    3) and save the "hard returns" with it...</textarea>
    <BR> <textarea name="textarea2 " cols="57" rows="5">and when I save
    that text in a cookie...
    It looks like the textarea underneath!</textarea> <BR>
    <textarea name="textarea3 " cols="57" rows="5">1) I want to save
    this text__2) in a cookie, __3) and save the "hard returns" with
    it...</textarea></td>
    </tr>
    <tr>
    <td height="6"></td>
    </tr>
    </table>
    </form>
    <form name="frm2">
    <table align="center" border="1" resize="none">
    <tr>
    <td align="center">
    <input type="button" name="first" value="|<< "
    onClick="naviga te('first');cou ntRecords()">
    <input type="button" name="previous" value=" < "
    onClick="naviga te('previous'); countRecords()" >
    <input type="button" name="next" value=" > "
    onClick="naviga te('next');coun tRecords()">
    <input type="button" name="last" value=" >>|"
    onClick="naviga te('last');coun tRecords()">
    <input type="box" name="actual" size=30>
    </td>
    </tr>
    <tr>
    <td align="center">
    <input type="button" name="add" value=" NEW "
    onClick="newRec ();countRecords ()">
    <input type="button" name="set" value="SAVE RECORD"
    onClick="setRec ();countRecords ()">
    <input type="button" name="del" value="Delete"
    onClick="delRec ();countRecords ()">
    </td>
    </tr>
    </table>
    </form>
    </center>
    </body></html>







    I know, when you want to save "hard returns" in a cookie, you need to
    escape() the value of a textarea when saving it, and here's how I've
    modifided that in the code:

    function setRec() {
    strRecord = "";
    for(i = 0; i < document.frm1.e lements.length; i++) {
    strRecord = strRecord + document.frm1.e lements[i].value + ":";
    }
    arrRecords[recCount] = strRecord;
    document.frm2.a dd.value = " NEW ";
    document.cookie = "Records="+esca pe(arrRecords)+ ";expires=" +
    expireDate.toGM TString();
    }



    Works fine but my big question is: where can I Unescape the value when
    loading the cookie?

    Hope anyone can help me out on this one,
    Thanks for any insight!

    Koen


  • Dr John Stockton

    #2
    Re: help please..how to unescape() the value of a cookie?

    JRS: In article <OAmNa.6395$7h. 12140@afrodite. telenet-ops.be>, seen in
    news:comp.lang. javascript, koen colen <koen.colen@pan dora.be> posted at
    Fri, 4 Jul 2003 21:46:22 :-
    [color=blue]
    >expireDate = new Date;
    >expireDate.set Date(expireDate .getDate()+365) ;[/color]

    That expires a day early 25% of the time.

    expireDate.setM onth(expireDate .getMonth()+12) ;

    That, at a cost of one more character, gives the right date without any
    getYear/getFullYear concern.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

    Comment

    Working...