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

Counting words in a webpage until click position

P: n/a
It's possible create a javascript which counts all words in a webpage
until the point in which I click with the mouse.

Any suggests?

Sep 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"regrat" <it********@yahoo.it> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
It's possible create a javascript which counts all words in a webpage
until the point in which I click with the mouse.

Any suggests?


Will this approach work for you? Watch for word-wrap.

<html>
<head>
<title>words.htm</title>
<script type="text/javascript">
function words() {
var oHTM = new ActiveXObject("htmlfile")
var sTXT = oHTM.parentWindow.clipboardData.getData("text");
if (sTXT == "") return;
var xTXT = sTXT + " ";
var yTXT = xTXT.replace(/^[^A-Za-z0-9]+/gi,"");
var zTXT = yTXT.replace(/[^A-Za-z0-9]+/gi," ");
var sMSG = "Selection has:";
sMSG += "\n" + sTXT.length + " bytes and";
sMSG += "\n" + (zTXT.split(" ").length-1) + " words.";
alert(sMSG);
}
</script>
</head>
<body>
<b>Select text, press Ctrl+C (copy to clipboard), then click "Count".</b>
<br><br>
<textarea cols="80" rows=10">
Four score and seven years ago our father brought forth upon this continent
a new nation ...
</textarea>
<br><br>
<input type="button" value="Count" onclick="words()">
<input type="reset" value="Clear">
</body>
</html>

<!-- Credit to:
http://javascript.internet.com/forms/word-count.html
-->
Sep 22 '05 #2

P: n/a
regrat wrote:
It's possible create a javascript which counts all words in a webpage
until the point in which I click with the mouse.

Any suggests?


Rudimentary solution for IE only (full documentation on the TextRange
object may be found at the Microsoft MSDN site):-

1. Use document.onclick to listen for mouse

2. onclick - Capture the the TextRange object using
document.selection.creatRange()

3. Use moveStart or other TextRange methods, to move the start of
the TextRange to the start of the HTML document.

4. Get the text, using TextRang.text property

5. Split the text, using regular expression suggested above. I.e.
var aArray=sText.split(/\b\w+\b/g);

6. Rough word count is aArray.length-1;

Sep 22 '05 #3

P: n/a
JRS: In article <Ku********************@comcast.com>, dated Thu, 22 Sep
2005 10:05:07, seen in news:comp.lang.javascript, McKirahan
<Ne**@McKirahan.com> posted :
"regrat" <it********@yahoo.it> wrote in message
news:11**********************@g14g2000cwa.googleg roups.com...
It's possible create a javascript which counts all words in a webpage
until the point in which I click with the mouse.

Any suggests?
Will this approach work for you? Watch for word-wrap.


Avoiding word-wrap is the article author's responsibility, as you have
been told before.

function words() {
var oHTM = new ActiveXObject("htmlfile")
For the Web, one should not offer Microsoft-specific "solutions", and I
suspect that is one.
var sTXT = oHTM.parentWindow.clipboardData.getData("text");
<textarea cols="80" rows=10">
Four score and seven years ago our father brought forth upon this continent
Only one father?
a new nation ...
</textarea>

That does not count "all words in a web page" as the OP asked, but
counts the words in the text on the clipboard.

Its interpretation of "words" does not correspond with normal usage;
that's a pity, and "that's" an example.

The following gives me the number of "words" in the displayed text of my
current Web page, but the OP will probably wish to improve the
definition of what a word is :-

document.write(document.body.innerText.split(/\s+/).length)

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of 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.
Sep 22 '05 #4

P: n/a
Dr John Stockton said the following on 9/22/2005 4:36 PM:
JRS: In article <Ku********************@comcast.com>, dated Thu, 22 Sep
2005 10:05:07, seen in news:comp.lang.javascript, McKirahan
<Ne**@McKirahan.com> posted :
"regrat" <it********@yahoo.it> wrote in message
news:11**********************@g14g2000cwa.google groups.com...
It's possible create a javascript which counts all words in a webpage
until the point in which I click with the mouse.

Any suggests?
Will this approach work for you? Watch for word-wrap.

Avoiding word-wrap is the article author's responsibility, as you have
been told before.
function words() {
var oHTM = new ActiveXObject("htmlfile")

For the Web, one should not offer Microsoft-specific "solutions", and I
suspect that is one.


Nah but its close. IE isn't the only browser to offer limited ActiveX
support on the web.

<snip>
The following gives me the number of "words" in the displayed text of my
current Web page, but the OP will probably wish to improve the
definition of what a word is :-

document.write(document.body.innerText.split(/\s+/).length)


You complain about an IE-only solution and then provide an IE only
solution? innerText is IE-only.

Nor does your "solution" address the issue of "that's" being one word or
two.

Neither solution seems to provide what the OP asked for though. That is
a count of words up to the point where the mouse was clicked, not how
many words are in the document.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 23 '05 #5

P: n/a
Randy Webb wrote:
Neither solution seems to provide what the OP asked for though. That is
a count of words up to the point where the mouse was clicked, not how
many words are in the document.


Here's an interim solution, if you want to count the words in a
selection (Ctrl-A for the whole document, otherwise highlight from the
top to the desired position):

function getSel()
{
if (document.getSelection)
txt = document.getSelection();
else if (document.selection)
txt = document.selection.createRange().text;
else
return;
alert(txt.split(/\s+/).length)
}

(where I've copied John's RegExp). Works in IE/Mozilla, when called by
a button click.

Nigel

--
ScriptMaster language resources (Chinese/Modern & Classical
Greek/IPA/Persian/Russian/Turkish):
http://www.elgin.free-online.co.uk

Sep 23 '05 #6

P: n/a
JRS: In article <eN********************@comcast.com>, dated Thu, 22 Sep
2005 22:40:14, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
The following gives me the number of "words" in the displayed text of my
current Web page, but the OP will probably wish to improve the
definition of what a word is :-

document.write(document.body.innerText.split(/\s+/).length)


You complain about an IE-only solution and then provide an IE only
solution? innerText is IE-only.

Nor does your "solution" address the issue of "that's" being one word or
two.

I supplied information, and indicated how it applied to my system; no
more. I clearly indicated that work on the definition of a word would
be needed; what is needed depends on the OP's application. If it is to
match the English concept, that's non-trivial.

--
John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Sep 23 '05 #7

P: n/a
Dr John Stockton said the following on 9/23/2005 4:22 PM:
JRS: In article <eN********************@comcast.com>, dated Thu, 22 Sep
2005 22:40:14, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
The following gives me the number of "words" in the displayed text of my
current Web page, but the OP will probably wish to improve the
definition of what a word is :-

document.write(document.body.innerText.split(/\s+/).length)


You complain about an IE-only solution and then provide an IE only
solution? innerText is IE-only.

Nor does your "solution" address the issue of "that's" being one word or
two.


I supplied information, and indicated how it applied to my system; no
more. I clearly indicated that work on the definition of a word would
be needed; what is needed depends on the OP's application. If it is to
match the English concept, that's non-trivial.


I see you are adept at ignoring my first statement. That is to be
expected of a hypocrite.

But not even your information, and how it applies to your system, answer
directly (nor indirectly) the OP's question.

You took McKirahan to task for offering an IE only solution, now I am
taking *you* to task for doing the same thing.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 23 '05 #8

P: n/a
regrat wrote:
It's possible create a javascript which counts all words in a webpage
until the point in which I click with the mouse.
Unlikely. Most certainly ugly inefficient, or proprietary and not
cross-browser.
Any suggests?


It is possible in Mozilla/5.0 and IE 4+ to count all words in a text
selection. If this is enough for you, ask Google Groups about retrieving
data from text selections.
PointedEars
Oct 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.