word counter script locks up Mozilla

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    word counter script locks up Mozilla

    Hey folks,

    I somehow managed to create a script that locks up Mozilla (1.3)
    tight. Here's the culprit, perhaps y'all can provide some insight
    into what the trouble is. (This script was cobbled together from
    several different word-count routines I found on the web, none of
    which worked correctly in all cases.)

    function trim(data)
    {
    i=0;
    while (/\s/.test(data.char At(i)))
    {data=data.subs tring(i,data.le ngth);}
    i=data.length;
    while (/\s/.test(data.char At(i))) {data=data.subs tring(1,i);}
    return data;
    }

    function countWords()
    {
    input=document. forms[0].elements[0].value;
    input=trim(inpu t);
    wordList=input. split(/\s+/g);
    output=wordList .length;
    return output;
    }

    activated via a button: onClick="alert( countWords())"

    Thanks for any help,
    --David
  • Randy Webb

    #2
    Re: word counter script locks up Mozilla

    David wrote:
    [color=blue]
    > Hey folks,
    >
    > I somehow managed to create a script that locks up Mozilla (1.3)
    > tight. Here's the culprit, perhaps y'all can provide some insight
    > into what the trouble is. (This script was cobbled together from
    > several different word-count routines I found on the web, none of
    > which worked correctly in all cases.)
    >
    > function trim(data)
    > {
    > i=0;
    > while (/\s/.test(data.char At(i)))
    > {data=data.subs tring(i,data.le ngth);}
    > i=data.length;
    > while (/\s/.test(data.char At(i))) {data=data.subs tring(1,i);}
    > return data;
    > }
    >
    > function countWords()
    > {
    > input=document. forms[0].elements[0].value;
    > input=trim(inpu t);
    > wordList=input. split(/\s+/g);
    > output=wordList .length;
    > return output;
    > }
    >
    > activated via a button: onClick="alert( countWords())"
    >[/color]

    I tested it, as you have it, in Moz1.4 and it worked flawlessly, so it
    must be something they changed in 1.3/1.4

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq/

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: word counter script locks up Mozilla

      david.lindsey@m indspring.com (David) writes:
      [color=blue]
      > I somehow managed to create a script that locks up Mozilla (1.3)
      > tight. Here's the culprit,[/color]
      ....[color=blue]
      > i=0;
      > while (/\s/.test(data.char At(i)))
      > {data=data.subs tring(i,data.le ngth);}[/color]

      If the first character of data is a space, the the test tests true,
      and then you execute
      data = data.substring( 0,data.length);
      which is the same as
      data = data
      Infinite loop => no response from browser.

      So, you don't remove the space. You loop never ends, as the next test
      also finds a space at the beginning.

      I guess it should be
      data = data.substring( 1,data.length);
      instead.
      [color=blue]
      > i=data.length;[/color]

      Then i is set to the length of data ....
      [color=blue]
      > while (/\s/.test(data.char At(i))) {data=data.subs tring(1,i);}[/color]

      and used as an index into the string. As an index, the length
      is one larger than the last character of the string, so
      data.charAt(i)
      gives the empty string, which doesn't match. You don't remove
      any space from the end of the string.

      So, your trim function is totally faulty: crashes on initial
      whitespace, and ignores termination.

      Try this one instead:

      function trim(str) {
      return str.replace(/^\s*/,"").replace (/\s*$/,"");
      }

      [color=blue]
      > function countWords()
      > {[/color]

      You should declare your local variables so you don't overwrite
      global ones:
      [color=blue]
      > input=document. forms[0].elements[0].value;[/color]
      var input = ...[color=blue]
      > input=trim(inpu t);
      > wordList=input. split(/\s+/g);[/color]
      var wordList = ...[color=blue]
      > output=wordList .length;[/color]
      var output = ...[color=blue]
      > return output;
      > }[/color]


      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • David

        #4
        Re: word counter script locks up Mozilla

        Randy Webb <hikksnotathome @aol.com> wrote in message news:<_Jydndxcv 4K6obLd4p2dnA@c omcast.com>...
        [color=blue]
        > I tested it, as you have it, in Moz1.4 and it worked flawlessly, so it
        > must be something they changed in 1.3/1.4[/color]

        I forgot to mention, the key to the lockup is a space before the first
        word in the textarea. Although after tweaking heavily I managed to
        keep the error from appearing and got the word counter working with
        everything I can think to throw at it, I'm still curious why this
        previous version crashes the browser. I would have thought that the
        javaScript engines would be "wise" enough to circumvent endless loops
        or other typical programming bugs with an error message or something.
        Naive to think so? Seems kinda risky for any old random
        poorly-crafted web script to be able to lock everything up like that.

        --David

        Comment

        • Dr John Stockton

          #5
          Re: word counter script locks up Mozilla

          JRS: In article <6769ced8.04021 42301.55f24163@ posting.google. com>, seen
          in news:comp.lang. javascript, David <david.lindsey@ mindspring.com>
          posted at Sat, 14 Feb 2004 23:01:57 :-
          [color=blue]
          > (This script was cobbled together from
          >several different word-count routines I found on the web, none of
          >which worked correctly in all cases.)[/color]

          That, of course, is the problem.

          First, one must define "word" -
          is door-mat one word or two?
          is cat's one word or two?
          how may words in cat's-paw?
          is Q a word?
          how many words in bb22cc?

          The simple case is if a word is any string of characters from one set,
          separated by any number of characters not in the set.

          For test purposes, let the good set be the characters matched by RegExp
          \w, so the bad set is matched by \W.

          S = "the quick brown fox "
          X = S.replace(/\w+/g, 'a').replace(/\W+/g, '').length

          gives X=4; there is no need to pre-trim S.

          If it is necessary to count words in many or long strings, do speed
          tests of different methods.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • David

            #6
            Re: word counter script locks up Mozilla

            Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<d68g5y58. fsf@hotpop.com> ...[color=blue]
            >
            > If the first character of data is a space, the the test tests true,[/color]
            ....[color=blue]
            > So, you don't remove the space. You loop never ends, as the next test
            > also finds a space at the beginning.[/color]

            Ah, so! My heavily tweaked script now seems to work great, even with
            mulitple whitespace characters encountered before, between, and after
            the words. I will post the complete, working script in another thread
            ("working word counter") for public reference.

            ....[color=blue]
            > You should declare your local variables so you don't overwrite
            > global ones:[/color]

            Good advice. Done and done. Thanks for the help.
            --David (still amazed that endless loops aren't trapped somehow by the
            browser)

            Comment

            • David

              #7
              Re: word counter script locks up Mozilla

              Dr John Stockton <spam@merlyn.de mon.co.uk> wrote in message news:<TU84ZFE1i 8LAFwg+@merlyn. demon.co.uk>...
              [color=blue]
              > First, one must define "word" -
              > is door-mat one word or two?
              > is cat's one word or two?
              > how may words in cat's-paw?
              > is Q a word?
              > how many words in bb22cc?[/color]
              <snip>

              Too true; for my purposes I settled on "any group of characters
              separated by whitespace" and used the regexp sequence /\s/ as the
              definition of whitespace characters (which I think includes space,
              carriage return, linefeed, tab, etc.). I will post the complete (now
              working) script in a new thread "working word counter". It worked
              instantaneously on an ~1800 word essay, which is what I originally
              wanted the counter for.

              Thanks for the help Dr. John!
              --David

              Comment

              Working...