M
mark leeds
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>
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>