Answers To Questions About JavaScript
Our primary focus at willmaster.com is CGI. However, we
do receive some questions about JavaScript developed for
Possibilities ezine articles and about JavaScript in
general.
Here are the answers to some JavaScript questions we have
received.
Question:
If I am validating a form AND preventing multiple clicks,
is the following code correct?
<input
type="text"
onClick="return count()"
onClick="ValidateAll()">
Thanks. Russell
Answer:
Only one onclick attribute may be used.
The use of several onclick attributes can be simulated
by including one of the onclick values in the function
of the other.
In your example, the count() function could include the
line:
ValidateAll();
For example:
function count() {
ValidateAll();
// function count() line
// function count() line
// function count() line
}
Then only the
onClick="return count()"
attribute is specified in the INPUT field:
<input
type="text"
onClick="return count()">
Question:
In the "No-Kill Pop Box (Instead of Popups)" JavaScript
code, can a cookie be used in the script to prevent the
layer from showing more than once on any web page. For
example, I might have the JavaScript on every page, but
I want it to show only once no matter how many pages they
visit. I haven't had much experience with cookies. Robert
Answer:
The "No-Kill Pop Box; Part III" article contains a cookie
solution. But the cookie has an effect only on the page
it's set from. For site-wide effect, find this line in
the JavaScript:
document.cookie = 'wpPopBox=displayed' + exp;
Immediately above that line, insert this line:
exp += "; path=/";
The inserted line will set the cookie path to your document
root, which has an effect on all pages of your web site.
If you wish to enhance your understanding of how cookies
work, the "One-Event Cookies" article and the JavaScript
code accompanying it might be of assistance.
Both of the above mentioned articles are linked at
/library/ in the
"Tutorials" column.
Question:
Hi, I'm a novice. I'm working on an "embedded webserver".
Everything's got to be very simple. I have a form with the
action being a cgi function. But I'd also like to have a
button on the page that calls another cgi function. I don't
want it to act like a submit button. I don't want to have
to redraw the page or redirect. Thanks for you help, Gwen
Answer:
To avoid redraw, you may need to use JavaScript instead
of CGI.
CGI, by definition, receives information from a browser
and returns information to a browser. Information flows
both ways.
The information sent to a browser is the content of a new
web page to be displayed.
The "How CGI Works" article at
/a/28t/pl.pl?howcgi
is a not-so-very technical overview of how CGI works.
Response:
Thanks for such a quick reply. I do want to get information
from the browser to the server. I just don't want the page
to disappear when I do. Can I do this with a javascript?
Can a javascript call a cgi function? If so can you give me
an example so that I get the syntax correct.
Answer:
When something is sent to the server using CGI, the server
must send something back. Otherwise, you get an Internal
Server Error because the CGI process couldn't be completed.
Here are 3 ways to send something to a CGI program without
having the browser re-load the page:
-
If you can predict the click, and the information
is available to send to the server when the page
first loads, have the CGI script generate it's web
page content with JavaScript document.writeln()
statements.
The returned content might be put into a DIV layer
with an "invisible" attribute that becomes "visible"
when the anticipated click becomes actual.
-
If you don't know what will be sent to the CGI
program until after the page has been loaded, when
it's time to send the information to the CGI program
it might be done as an image request. A JavaScript
function could create a new image class and then
assign the SRC, the SRC being the URL to the CGI
program with information sent as parameters.
-
Another way is with an IFRAME. When the CGI program
is called, only the page in the IFRAME reloads.
Question:
The tutorial in Possibilities Issue # 80: "Putting Form
Confirmation Pages Into Popups" is great. When using radio
buttons and checkboxes, the form works okay but the
JavaScript line
constructURL('content11',document.myform.content11.value);
sends the value of the field whether or not the field was
checked. How can I have the value of the field sent only
when it's been checked? Wilhelm
Answer:
As you noticed, radio buttons and checkboxes need somewhat
different handling to retrieve their values. Each radio
button and checkbox must first be tested to see whether
or not it is checked. Examples:
if(document.FormName.CheckName.checked == true) {
constructURL('CheckName',document.FormName.CheckName.value);
}
if(document.FormName.RadioName[0].checked == true) {
constructURL('RadioName',document.FormName.RadioName.value);
}
if(document.FormName.RadioName[1].checked == true) {
constructURL('RadioName',document.FormName.RadioName.value);
}
Replace every occurrance of CheckName with the checkbox
field name and every occurrance of RadioName with your
radio button field names. Also, replace every occurrance
of FormName with the name you gave your form.
Question:
How do I use radio-buttons to activate selective fields on
a form using perl/cgi?
For example, if there are two choices with a submit -- no
and yes. The "yes" option is associated with a text input
field and "no" has nothing associated to it. If I select
"no", it should deactivate the text box and not allow the
user to type into it. If I select "yes" the text box should
be active to take input. Srinivas
Answer:
This is probably best handled with JavaScript. Whenever
a form field becomes "in focus" or is "blurred," or when
the submit button is clicked, your JavaScript can check
which radio button is checked and, if appropriate, blank
the text field.
The reason CGI is not suitable is because the form must be
submitted before the program can do anything with it. What
you need, in this case, is something that will do the
checking before the form is submitted. JavaScript is good
for that.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2004 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.