word counter script locks up Mozilla

R

Randy Webb

David said:
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.charAt(i)))
{data=data.substring(i,data.length);}
i=data.length;
while (/\s/.test(data.charAt(i))) {data=data.substring(1,i);}
return data;
}

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

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

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
 
D

David

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.charAt(i)))
{data=data.substring(i,data.length);}
i=data.length;
while (/\s/.test(data.charAt(i))) {data=data.substring(1,i);}
return data;
}

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

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

Thanks for any help,
--David
 
L

Lasse Reichstein Nielsen

I somehow managed to create a script that locks up Mozilla (1.3)
tight. Here's the culprit, ....
i=0;
while (/\s/.test(data.charAt(i)))
{data=data.substring(i,data.length);}

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.
i=data.length;

Then i is set to the length of data ....
while (/\s/.test(data.charAt(i))) {data=data.substring(1,i);}

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*$/,"");
}

function countWords()
{

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


/L
 
D

David

Randy Webb said:
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

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
(This script was cobbled together from
several different word-count routines I found on the web, none of
which worked correctly in all cases.)

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.
 
D

David

Lasse Reichstein Nielsen said:
If the first character of data is a space, the the test tests true, ....
So, you don't remove the space. You loop never ends, as the next test
also finds a space at the beginning.

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.

....
You should declare your local variables so you don't overwrite
global ones:

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

David

Dr John Stockton said:
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?
<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top