(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[i].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.'