473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.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
Jul 20 '05 #1
6 1667
David wrote:
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

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

Jul 20 '05 #2
da***********@mindspring.com (David) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Randy Webb <hi************@aol.com> wrote in message news:<_J********************@comcast.com>...
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
Jul 20 '05 #4
JRS: In article <67**************************@posting.google.com >, seen
in news:comp.lang.javascript, David <da***********@mindspring.com>
posted at Sat, 14 Feb 2004 23:01:57 :-
(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.

--
© 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.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #5
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<d6**********@hotpop.com>...

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)
Jul 20 '05 #6
Dr John Stockton <sp**@merlyn.demon.co.uk> wrote in message news:<TU**************@merlyn.demon.co.uk>...
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
Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: reneecccwest | last post by:
Hello, can anybody share the code for a word counter for the textarea user input? what if max characters are 256? thanks
2
by: David | last post by:
After getting some help working through my bugs, I have what seems to be a robust, working word counter script. I post it here to benefit others that might want this in the future and so that if I...
8
by: William Starr Moake | last post by:
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...
8
by: Prometheus Research | last post by:
http://newyork.craigslist.org/eng/34043771.html We need a JavaScript component which will auto-submit a form after a set period has elapsed. The component must display a counter that dynamically...
3
by: glevik | last post by:
Hello, Anyone that can think of a way to programmaticaly determine the word on an HTML page that the user clicked on will be my hero for life. Leo
11
by: OB1 | last post by:
I have seven computers. I have several web pages for the classes I teach at the local Community college. Some of my student complain to me that the counter on my web page is always at zero. On...
2
by: Anouar | last post by:
The point was to make a program that gives you the possibilty to enter a sentence and when you press "enter" it should ask a number for example when i press 4 It should take the 4th word out of...
7
by: onetitfemme | last post by:
Hi, for some reason Firefox is apparently inserting a new line somehow after some IPA symbols which I have written in their hexadecimal notation. Also, I would like to have two spaces by...
6
by: boyindie86 | last post by:
Hi I have been fighting with this lump of code for the last week what I am trying to do is that I am passing words into passages of texts, and I want the system to go and find exact word matches...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.