download counter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Starr Moake

    download counter

    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="JavaS cript">
    function addClicks() {
    total = "form1.valu e";
    form1.value = (eval(total.val ue) + (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="addCli cks();" width="10"></p></form>

    What am I doing wrong?
  • mscir

    #2
    Re: download counter

    William Starr Moake wrote:[color=blue]
    > 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="JavaS cript">
    > function addClicks() {
    > total = "form1.valu e";
    > form1.value = (eval(total.val ue) + (1));
    > }
    > </script>[/color]

    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="JavaS cript">
    function addClicks() {
    var formtotal=docum ent.forms("form 1").total
    formtotal.value = +formtotal.valu e + 1;
    }
    </script>

    Mike

    Comment

    • Richard Cornford

      #3
      Re: download counter

      mscir wrote:[color=blue]
      > William Starr Moake wrote:[color=green]
      >> 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="JavaS cript">
      >> function addClicks() {
      >> total = "form1.valu e";[/color][/color]

      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).
      [color=blue][color=green]
      >> form1.value = (eval(total.val ue) + (1));[/color][/color]

      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).
      [color=blue][color=green]
      >> }
      >> </script>[/color]
      >
      > I've read in this group that "language" is no longer
      > recommended, and eval should be avoided when possible.[/color]

      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.
      [color=blue]
      > Also, you weren't changing the
      > value of the textbox but of the form, maybe try something like this:
      >
      > <script language="JavaS cript">[/color]
      <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">
      [color=blue]
      > var formtotal=docum ent.forms("form 1").total[/color]
      ^ ^
      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.


      Comment

      • mscir

        #4
        Re: download counter

        Richard Cornford wrote:[color=blue]
        > mscir wrote:[/color]
        <snip>[color=blue][color=green]
        >>I've read in this group that "language" is no longer
        >>recommended , and eval should be avoided when possible.[/color]
        >
        > 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.
        >
        >[color=green]
        >>Also, you weren't changing the
        >>value of the textbox but of the form, maybe try something like this:
        >><script language="JavaS cript">[/color]
        >
        > 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">
        >[color=green]
        >> var formtotal=docum ent.forms("form 1").total[/color]
        > ^ ^
        > 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.[/color]

        Thanks Richard,
        Mike

        Comment

        • William Starr Moake

          #5
          Re: download counter

          On Tue, 13 Apr 2004 23:16:47 -0700, mscir
          <mscir@access4l ess.com.net.org .uk> wrote:
          [color=blue]
          >William Starr Moake wrote:[color=green]
          >> 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="JavaS cript">
          >> function addClicks() {
          >> total = "form1.valu e";
          >> form1.value = (eval(total.val ue) + (1));
          >> }
          >> </script>[/color]
          >
          >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="JavaS cript">
          >function addClicks() {
          > var formtotal=docum ent.forms("form 1").total
          > formtotal.value = +formtotal.valu e + 1;
          >}
          ></script>
          >[/color]
          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.

          Comment

          • mscir

            #6
            Re: download counter

            William Starr Moake wrote:[color=blue]
            > 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.[/color]

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

            Mike

            Comment

            • William Starr Moake

              #7
              Re: download counter

              On Wed, 14 Apr 2004 21:07:43 -0700, mscir
              <mscir@access4l ess.com.net.org .uk> wrote:
              [color=blue]
              >William Starr Moake wrote:[color=green]
              >> 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.[/color]
              >
              >Right, I looked at the code without considering the real problem, sorry
              >for any wasted time.
              >[/color]
              No waste. At least your script counted the download clicks. I learned
              something.

              Comment

              • MasonC

                #8
                Re: download counter

                On Wed, 14 Apr 2004 17:09:23 -1000, William Starr Moake <wsmoake@yahoo. com> wrote:
                [color=blue]
                >On Tue, 13 Apr 2004 23:16:47 -0700, mscir
                ><mscir@access4 less.com.net.or g.uk> wrote:
                >[color=green]
                >>William Starr Moake wrote:[color=darkred]
                >>> 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="JavaS cript">
                >>> function addClicks() {
                >>> total = "form1.valu e";
                >>> form1.value = (eval(total.val ue) + (1));
                >>> }
                >>> </script>[/color]
                >>
                >>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="JavaS cript">
                >>function addClicks() {
                >> var formtotal=docum ent.forms("form 1").total
                >> formtotal.value = +formtotal.valu e + 1;
                >>}
                >></script>
                >>[/color]
                >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:[/color]

                you have to use[color=blue]
                >server-side scripting to store the current total so that it's
                >displayed for the next person who accesses the page.[/color]

                How about storing it in a cookie?

                Mason C

                Comment

                • Grant Wagner

                  #9
                  Re: download counter

                  MasonC wrote:
                  [color=blue]
                  > On Wed, 14 Apr 2004 17:09:23 -1000, William Starr Moake <wsmoake@yahoo. com> wrote:
                  >[color=green]
                  > >On Tue, 13 Apr 2004 23:16:47 -0700, mscir
                  > ><mscir@access4 less.com.net.or g.uk> wrote:
                  > >[color=darkred]
                  > >>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="JavaS cript">
                  > >>> function addClicks() {
                  > >>> total = "form1.valu e";
                  > >>> form1.value = (eval(total.val ue) + (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="JavaS cript">
                  > >>function addClicks() {
                  > >> var formtotal=docum ent.forms("form 1").total
                  > >> formtotal.value = +formtotal.valu e + 1;
                  > >>}
                  > >></script>
                  > >>[/color]
                  > >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:[/color]
                  >
                  > you have to use[color=green]
                  > >server-side scripting to store the current total so that it's
                  > >displayed for the next person who accesses the page.[/color]
                  >
                  > How about storing it in a cookie?
                  >
                  > Mason C[/color]

                  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 <gwagner@agrico reunited.com>

                  * Client-side Javascript and Netscape 4 DOM Reference available at:
                  *

                  * 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


                  Comment

                  Working...