a reasonably simple program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mark leeds

    a reasonably simple program

    i am not a javasript programmer by any stretch but
    i have been writing a javascript programmer for
    a friend that does the following :

    1) prompts the user for first name, middle name and last name

    2) saves the data in a cookie

    3) spits the cookie data back out to the respective fields

    i think it's very close to working but
    i am getting an error. i don't have a javascript debugger
    (nor would i know how to use one if i did have it because
    this is my first javascript program )
    so i am pretty stuck and i found this website.

    i would really appreciate it if someone could look at it
    and let me know if they see the problem ?
    if you are reasonably decent at javascript,
    it probably wouldn't take very long. i'm not
    so familar with this language.

    thanks a lot.


    the code is below.

    ----------------------------------------------------------------------

    <html>
    <head><title> A Cookie Example </title>
    <script language="JavaS cript">

    function getCookieVal(na me,index) {
    name = name + "=";
    var ck = document.cookie ;

    document.write( document.cookie )

    if ( ck.length > 0) {
    firstcharpos = ck.indexOf(name );

    if ( firstcharpos != -1){
    endrecord = ck.indexOf(";", firstcharpos+na me.length);

    if(endrecord == -1) {
    endrecord = ck.length;
    }


    var lastindexpos = firstcharpos + name.length;
    // scan for separator bars
    for (i=0; i<index;i++) {
    if (i!=0){lastinde xpos++}
    firstindexpos = lastindexpos;
    lastindexpos = ck.indexOf("|", firstindexpos);
    }

    // if we can't find another bar then go to the end of the record

    if ( lastindexpos == -1) {
    lastindexpos = endrecord;
    }

    document.write( " " + "<br>")
    // scan last entry for semi colons. we may find a bar but it could
    belong to the next entry

    document.write( "first index position is " + firstindexpos + " last is
    " + lastindexpos + "<br>");

    substring = ck.substring(fi rstindexpos,las tindexpos);
    sc = substring.index Of(";");
    if (sc==-1){
    return(unescape (ck.substring(f irstindexpos,la stindexpos)));
    } else {
    return(unescape (ck.substring(f irstindexpos,en drecord)));
    }
    }
    }
    return null;
    }


    function setCookie(name, form) {

    var combined_string = form.fnameCooki e.value + "|" +
    form.mnameCooki e.value + "|" + form.lnameCooki e.value;
    document.cookie = "name=" + combined_string + ";";
    document.write( combined_string )
    document.write( document.cookie )

    form.fnameCooki e.value="";
    form.mnameCooki e.value="";
    form.lnameCooki e.value="";
    document.write( document.cookie )

    }

    function showCookie(form ) {
    form.fnameCooki e.value=getCook ieVal("Cookie", 1)
    document.write( form.fnameCooki e.value)
    form.mnameCooki e.value=getCook ieVal("Cookie", 2)
    form.lnameCooki e.value=getCook ieVal("Cookie", 3)

    }
    </script>
    </head>
    <body bgcolor="#CCFFF F"><center>
    <h2>A Cookie Example</h2>
    <form>
    <p>Please enter text to set the first name<br>
    <input type="text" name="fnameCook ie" value="" size=50>

    <p>Please enter text to set the middle name<br>
    <input type="text" name="mnameCook ie" value="" size=50>

    <p>Please enter text to set the last name<br>
    <input type="text" name="lnameCook ie" value="" size=50>

    <p>Click on this button to save the cookie <br><br>
    <input type="button" value="Create Cookie" name="SetButton "
    onClick="setCoo kie('Cookie', this.form);">

    <p>Now click on this button to show the values in the text boxes
    <br><br>
    <input type="button" value="Display Cookie" name="DisplayBu tton"
    onClick="showCo okie(this.form) ;">
    </form>
    </center>
    </body>
    </html>
  • Lasse Reichstein Nielsen

    #2
    Re: a reasonably simple program

    mleeds@mlp.com (mark leeds) writes:
    [color=blue]
    > i am not a javasript programmer by any stretch but i have been
    > writing a javascript programmer for a friend that does the following :
    >
    > 1) prompts the user for first name, middle name and last name
    >
    > 2) saves the data in a cookie
    >
    > 3) spits the cookie data back out to the respective fields[/color]

    Ok, that sounds doable.
    [color=blue]
    > i think it's very close to working but
    > i am getting an error. i don't have a javascript debugger
    > (nor would i know how to use one if i did have it because
    > this is my first javascript program )[/color]

    You don't *need* a debugger. You *do* need to tell us what the error
    message that you get, is.
    [color=blue]
    > so i am pretty stuck and i found this website.[/color]

    This is not a web site. It is a newsgroup. It has nothing to
    do with the web at all, except the subject :)
    [color=blue]
    > i would really appreciate it if someone could look at it
    > and let me know if they see the problem ?[/color]

    I see several potential problems, and one that is definitly wrong.
    The definite error is that you set the cookie with the name "name"
    and try to read it again with the name "Cookie".
    [color=blue]
    > if you are reasonably decent at javascript,
    > it probably wouldn't take very long. i'm not
    > so familar with this language.[/color]
    [color=blue]
    > ----------------------------------------------------------------------[/color]

    I'll be pedantic. It's for your own good. ... Ok, it's because I like it :)

    I recommend having a <!DOCTYPE> declaration at the beginning of the
    document. It is required in a valid HTML 4 document, and it allows you
    to validate the HTML with online validators.
    [color=blue]
    > <html>
    > <head><title> A Cookie Example </title>
    > <script language="JavaS cript">[/color]

    In HTML 4, the "type" attribute is required, and the "language"
    attribute is deprecated. I.e., the recommended opening script
    tag is:
    <script type="text/javascript">
    [color=blue]
    > function getCookieVal(na me,index) {
    > name = name + "=";
    > var ck = document.cookie ;
    >
    > document.write( document.cookie )[/color]

    Do you mean to use document.write here, or is it just something you
    added during debugging? I recommend using alert instead, it can't
    overwrite the entire document. Calling document.write after the page
    has finished loaded *will* delete the entire page. That will probably
    make a lot of things break.

    Lose the document.write' s and use alert instead. (For a quick fix:
    document.write = alert;
    )

    You don't declare "firstcharp os" and "endrecord" as local variables,
    so they become global variables instead. Probably an oversigt, since
    you declare the other variables correctly.
    [color=blue]
    > if ( ck.length > 0) {
    > firstcharpos = ck.indexOf(name );
    >
    > if ( firstcharpos != -1){
    > endrecord = ck.indexOf(";", firstcharpos+na me.length);
    >
    > if(endrecord == -1) {
    > endrecord = ck.length;
    > }[/color]

    Here you look for "|"-bars. The indices starts at 1, right?
    [color=blue]
    > var lastindexpos = firstcharpos + name.length;
    > // scan for separator bars
    > for (i=0; i<index;i++) {
    > if (i!=0){lastinde xpos++}
    > firstindexpos = lastindexpos;
    > lastindexpos = ck.indexOf("|", firstindexpos);
    > }[/color]
    .....[color=blue]
    > if (sc==-1){
    > return(unescape (ck.substring(f irstindexpos,la stindexpos)));
    > } else {
    > return(unescape (ck.substring(f irstindexpos,en drecord)));[/color]

    You unescape the result but doesn't escape the cookie when you set it.
    However, you need to do it in the correct order, since "|"'s are also
    escaped.


    I can't really wrap my head around all these indices. There is
    probably nothing wrong with it, but it is far too confuzing for me to
    make sure right now.

    All this parsing can be done easier with some split functions:

    ---
    function getCookieVal(na me,index) {
    var cookies = document.cookie s.split(";");
    for (var i=0 ; i<cookies.lengt h ; i++) {
    var cookie = cookies[i].split("=");
    if (cookie[0]==name) {
    var data = unescape(cookie[1]).split("|");
    return data[index-1];
    }
    }
    return null;
    }
    ---

    To split is divine! :)
    [color=blue]
    > function setCookie(name, form) {
    >
    > var combined_string = form.fnameCooki e.value + "|" +
    > form.mnameCooki e.value + "|" + form.lnameCooki e.value;
    > document.cookie = "name=" + combined_string + ";";[/color]

    You set the cookie with the name "name", but later read it with the
    name "Cookie"! This is the error.

    Remember to escape the cookie value!

    You might want to set the exiration date on the cookie.

    [color=blue]
    > function showCookie(form ) {
    > form.fnameCooki e.value=getCook ieVal("Cookie", 1)[/color]

    Here, you use the name "Cookie" to get the cookie again, not "name".
    [color=blue]
    > document.write( form.fnameCooki e.value)
    > form.mnameCooki e.value=getCook ieVal("Cookie", 2)
    > form.lnameCooki e.value=getCook ieVal("Cookie", 3)
    >
    > }
    > </script>
    > </head>
    > <body bgcolor="#CCFFF F"><center>[/color]

    The "bgcolor" attribute and the "center" element are both deprecated
    in HTML 4. The recommended way to get the same effect is to use CSS:
    <body style="backgrou nd-color:#CCFFFF;t ext-align:center;">


    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...