473,666 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>
Jul 20 '05 #1
1 2664
ml****@mlp.com (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="JavaS cript">
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(na me,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 "firstcharp os" 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+na me.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){lastinde xpos++}
firstindexpos = lastindexpos;
lastindexpos = ck.indexOf("|", firstindexpos);
} ..... if (sc==-1){
return(unescape (ck.substring(f irstindexpos,la stindexpos)));
} else {
return(unescape (ck.substring(f irstindexpos,en drecord)));
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! :)
function setCookie(name, form) {

var combined_string = form.fnameCooki e.value + "|" +
form.mnameCooki e.value + "|" + form.lnameCooki e.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.fnameCooki e.value=getCook ieVal("Cookie", 1)
Here, you use the name "Cookie" to get the cookie again, not "name".
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>


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2247
by: Chris Vinall | last post by:
I'm an SQL beginner and this is driving me nuts as it seems simple enough but I can't figure it out. I have a table that looks like: ID: int MajorVersion: int MinorVersion: int Content: ntext
17
6512
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual interest rate I, and annual service charge S. Your algorithm would then compute and print out the total amount of interest earned during the year and the final account balance at the end of the year (assuming that interest is compounded monthly, and...
11
2700
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
5
7236
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
10
2367
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when there is such information to be displayed are they coming from imported files, database ? Where and how this type of information is stored ? What is the way to retrieve such information in order to display it in page ?
30
3513
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
1
2066
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is...
17
5807
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8780
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6189
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.