By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,711 Members | 2,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,711 IT Pros & Developers. It's quick & easy.

download counter

P: n/a
I'm trying to script a download counter that will display the total
number on the download page without server-side scripting.

Below is my very incomplete beginning. It returns no errors, but does
nothing:

<script language="JavaScript">
function addClicks() {
total = "form1.value";
form1.value = (eval(total.value) + (1));
}
</script>

<form name="form1" method="get">
<p><input type="text" name="total" maxlength="4" size="4"> <br>
<input type="button" name="addition" value="Download"
onclick="addClicks();" width="10"></p></form>

What am I doing wrong?
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
William Starr Moake wrote:
I'm trying to script a download counter that will display the total
number on the download page without server-side scripting.
Below is my very incomplete beginning. It returns no errors, but does
nothing:
<script language="JavaScript">
function addClicks() {
total = "form1.value";
form1.value = (eval(total.value) + (1));
}
</script>


I've read in this group that "language" is no longer recommended, and
eval should be avoided when possible. Also, you weren't changing the
value of the textbox but of the form, maybe try something like this:

<script language="JavaScript">
function addClicks() {
var formtotal=document.forms("form1").total
formtotal.value = +formtotal.value + 1;
}
</script>

Mike

Jul 23 '05 #2

P: n/a
mscir wrote:
William Starr Moake wrote:
I'm trying to script a download counter that will display the total
number on the download page without server-side scripting.
Below is my very incomplete beginning. It returns no errors, but does
nothing:
<script language="JavaScript">
function addClicks() {
total = "form1.value";
The identifier - total - is being used in a way that would access a
global variable (or create one if one does not already exist as appears
to be the case). Variables should never be given more scope than they
need (general programming principle) and in this case - total - looks
like it should be local to the function (declared as - var total).
form1.value = (eval(total.value) + (1));
As - total - has been assigned a string value, the resolution of the
property accessor - total.value - will internally type-convert - total -
into a corresponding String object and then read a - value - property
from that String object. The result will be - undefined - (as String
objects have no - value - property), and therefor not of string type.
The eval function returns non-string arguments unaltered, so passing it
an undefined value is pointless (as it will do nothing but return that
value), but it also demonstrates a failure to even comprehend the
purpose of the - eval - function. (not that - eval - has any role in
this operation anyway).
}
</script>
I've read in this group that "language" is no longer
recommended, and eval should be avoided when possible.


You have got the stress the wrong way around, we say - eval - should
*only* be used when it is *necessary* (and that it is almost never
necessary). To say "avoided when possible" would imply that ignorance of
the alternatives would justify its use, while in reality it should only
be used when there is certainty that there are no alternatives.
Also, you weren't changing the
value of the textbox but of the form, maybe try something like this:

<script language="JavaScript"> <snip>

It seems odd to point out that the language attribute is not recommended
(because it is deprecated, and redundant when the required type
attribute is used) and then use it (and particularly without a type
attribute).

<script type="text/javascript">
var formtotal=document.forms("form1").total

^ ^
Parenthesising arguments for the - forms - collection is an IE-ism that
is not supported in many other browsers. The form of the expression is a
bracket notation property accessor, so the brackets should be square.

Richard.
Jul 23 '05 #3

P: n/a
Richard Cornford wrote:
mscir wrote:

<snip>
I've read in this group that "language" is no longer
recommended, and eval should be avoided when possible.


You have got the stress the wrong way around, we say - eval - should
*only* be used when it is *necessary* (and that it is almost never
necessary). To say "avoided when possible" would imply that ignorance of
the alternatives would justify its use, while in reality it should only
be used when there is certainty that there are no alternatives.

Also, you weren't changing the
value of the textbox but of the form, maybe try something like this:
<script language="JavaScript">


It seems odd to point out that the language attribute is not recommended
(because it is deprecated, and redundant when the required type
attribute is used) and then use it (and particularly without a type
attribute).

<script type="text/javascript">
var formtotal=document.forms("form1").total

^ ^
Parenthesising arguments for the - forms - collection is an IE-ism that
is not supported in many other browsers. The form of the expression is a
bracket notation property accessor, so the brackets should be square.


Thanks Richard,
Mike

Jul 23 '05 #4

P: n/a
On Tue, 13 Apr 2004 23:16:47 -0700, mscir
<ms***@access4less.com.net.org.uk> wrote:
William Starr Moake wrote:
I'm trying to script a download counter that will display the total
number on the download page without server-side scripting.
Below is my very incomplete beginning. It returns no errors, but does
nothing:
<script language="JavaScript">
function addClicks() {
total = "form1.value";
form1.value = (eval(total.value) + (1));
}
</script>


I've read in this group that "language" is no longer recommended, and
eval should be avoided when possible. Also, you weren't changing the
value of the textbox but of the form, maybe try something like this:

<script language="JavaScript">
function addClicks() {
var formtotal=document.forms("form1").total
formtotal.value = +formtotal.value + 1;
}
</script>

Your script works and actually retains the total when the page is
refreshed. BUT when the page is closed and reopened the total goes
back to zero. I guess everyone else was right: you have to use
server-side scripting to store the current total so that it's
displayed for the next person who accesses the page.

Thanks anyway.

Jul 23 '05 #5

P: n/a
William Starr Moake wrote:
Your script works and actually retains the total when the page is
refreshed. BUT when the page is closed and reopened the total goes
back to zero. I guess everyone else was right: you have to use
server-side scripting to store the current total so that it's
displayed for the next person who accesses the page.


Right, I looked at the code without considering the real problem, sorry
for any wasted time.

Mike

Jul 23 '05 #6

P: n/a
On Wed, 14 Apr 2004 21:07:43 -0700, mscir
<ms***@access4less.com.net.org.uk> wrote:
William Starr Moake wrote:
Your script works and actually retains the total when the page is
refreshed. BUT when the page is closed and reopened the total goes
back to zero. I guess everyone else was right: you have to use
server-side scripting to store the current total so that it's
displayed for the next person who accesses the page.


Right, I looked at the code without considering the real problem, sorry
for any wasted time.

No waste. At least your script counted the download clicks. I learned
something.
Jul 23 '05 #7

P: n/a
On Wed, 14 Apr 2004 17:09:23 -1000, William Starr Moake <ws*****@yahoo.com> wrote:
On Tue, 13 Apr 2004 23:16:47 -0700, mscir
<ms***@access4less.com.net.org.uk> wrote:
William Starr Moake wrote:
I'm trying to script a download counter that will display the total
number on the download page without server-side scripting.
Below is my very incomplete beginning. It returns no errors, but does
nothing:
<script language="JavaScript">
function addClicks() {
total = "form1.value";
form1.value = (eval(total.value) + (1));
}
</script>
I've read in this group that "language" is no longer recommended, and
eval should be avoided when possible. Also, you weren't changing the
value of the textbox but of the form, maybe try something like this:

<script language="JavaScript">
function addClicks() {
var formtotal=document.forms("form1").total
formtotal.value = +formtotal.value + 1;
}
</script>

Your script works and actually retains the total when the page is
refreshed. BUT when the page is closed and reopened the total goes
back to zero. I guess everyone else was right:


you have to useserver-side scripting to store the current total so that it's
displayed for the next person who accesses the page.


How about storing it in a cookie?

Mason C
Jul 23 '05 #8

P: n/a
MasonC wrote:
On Wed, 14 Apr 2004 17:09:23 -1000, William Starr Moake <ws*****@yahoo.com> wrote:
On Tue, 13 Apr 2004 23:16:47 -0700, mscir
<ms***@access4less.com.net.org.uk> wrote:
William Starr Moake wrote:
I'm trying to script a download counter that will display the total
number on the download page without server-side scripting.
Below is my very incomplete beginning. It returns no errors, but does
nothing:
<script language="JavaScript">
function addClicks() {
total = "form1.value";
form1.value = (eval(total.value) + (1));
}
</script>

I've read in this group that "language" is no longer recommended, and
eval should be avoided when possible. Also, you weren't changing the
value of the textbox but of the form, maybe try something like this:

<script language="JavaScript">
function addClicks() {
var formtotal=document.forms("form1").total
formtotal.value = +formtotal.value + 1;
}
</script>

Your script works and actually retains the total when the page is
refreshed. BUT when the page is closed and reopened the total goes
back to zero. I guess everyone else was right:


you have to use
server-side scripting to store the current total so that it's
displayed for the next person who accesses the page.


How about storing it in a cookie?

Mason C


Storing the value in a cookie will allow you to retain information about how many
times that specific user clicked the button/link, it won't allow you to count the
total number of clicks done by everyone.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.