a reasonably simple program

Discussion in 'Javascript' started by mark leeds, Oct 31, 2003.

  1. mark leeds

    mark leeds Guest

    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="JavaScript">

    function getCookieVal(name,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+name.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){lastindexpos++}
    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(firstindexpos,lastindexpos);
    sc = substring.indexOf(";");
    if (sc==-1){
    return(unescape (ck.substring(firstindexpos,lastindexpos)));
    } else {
    return(unescape (ck.substring(firstindexpos,endrecord)));
    }
    }
    }
    return null;
    }


    function setCookie(name, form) {

    var combined_string = form.fnameCookie.value + "|" +
    form.mnameCookie.value + "|" + form.lnameCookie.value;
    document.cookie = "name=" + combined_string + ";";
    document.write(combined_string)
    document.write(document.cookie)

    form.fnameCookie.value="";
    form.mnameCookie.value="";
    form.lnameCookie.value="";
    document.write(document.cookie)

    }

    function showCookie(form) {
    form.fnameCookie.value=getCookieVal("Cookie",1)
    document.write(form.fnameCookie.value)
    form.mnameCookie.value=getCookieVal("Cookie",2)
    form.lnameCookie.value=getCookieVal("Cookie",3)

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

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

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

    <p>Click on this button to save the cookie <br><br>
    <input type="button" value="Create Cookie" name="SetButton"
    onClick="setCookie('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="DisplayButton"
    onClick="showCookie(this.form);">
    </form>
    </center>
    </body>
    </html>
     
    mark leeds, Oct 31, 2003
    #1
    1. Advertising

  2. (mark leeds) writes:

    > 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


    Ok, that sounds doable.

    > 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 )


    You don't *need* a debugger. You *do* need to tell us what the error
    message that you get, is.

    > so i am pretty stuck and i found this website.


    This is not a web site. It is a newsgroup. It has nothing to
    do with the web at all, except the subject :)

    > i would really appreciate it if someone could look at it
    > and let me know if they see the problem ?


    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".

    > if you are reasonably decent at javascript,
    > it probably wouldn't take very long. i'm not
    > so familar with this language.


    > ----------------------------------------------------------------------


    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.

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


    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">

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


    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 "firstcharpos" and "endrecord" as local variables,
    so they become global variables instead. Probably an oversigt, since
    you declare the other variables correctly.

    > if ( ck.length > 0) {
    > firstcharpos = ck.indexOf(name);
    >
    > if ( firstcharpos != -1){
    > endrecord = ck.indexOf(";",firstcharpos+name.length);
    >
    > if(endrecord == -1) {
    > endrecord = ck.length;
    > }


    Here you look for "|"-bars. The indices starts at 1, right?

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

    .....
    > if (sc==-1){
    > return(unescape (ck.substring(firstindexpos,lastindexpos)));
    > } else {
    > return(unescape (ck.substring(firstindexpos,endrecord)));


    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(name,index) {
    var cookies = document.cookies.split(";");
    for (var i=0 ; i<cookies.length ; i++) {
    var cookie = cookies.split("=");
    if (cookie[0]==name) {
    var data = unescape(cookie[1]).split("|");
    return data[index-1];
    }
    }
    return null;
    }
    ---

    To split is divine! :)

    > function setCookie(name, form) {
    >
    > var combined_string = form.fnameCookie.value + "|" +
    > form.mnameCookie.value + "|" + form.lnameCookie.value;
    > document.cookie = "name=" + combined_string + ";";


    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.


    > function showCookie(form) {
    > form.fnameCookie.value=getCookieVal("Cookie",1)


    Here, you use the name "Cookie" to get the cookie again, not "name".

    > document.write(form.fnameCookie.value)
    > form.mnameCookie.value=getCookieVal("Cookie",2)
    > form.lnameCookie.value=getCookieVal("Cookie",3)
    >
    > }
    > </script>
    > </head>
    > <body bgcolor="#CCFFFF"><center>


    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="background-color:#CCFFFF;text-align:center;">


    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Oct 31, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    441
    Otis Mukinfus
    Jan 29, 2006
  2. Roedy Green

    Class.forName reasonably efficient

    Roedy Green, Jan 14, 2006, in forum: Java
    Replies:
    4
    Views:
    467
    Dave Glasser
    Jan 15, 2006
  3. Larry goodman

    reasonably priced reporting solution

    Larry goodman, Oct 15, 2003, in forum: Python
    Replies:
    5
    Views:
    363
    Larry goodman
    Oct 16, 2003
  4. Bruno Desthuilliers
    Replies:
    5
    Views:
    401
    Bruno Desthuilliers
    Aug 29, 2007
  5. ruby talk
    Replies:
    3
    Views:
    122
    Florian Frank
    Jan 27, 2005
Loading...

Share This Page