472,334 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Counting words in a webpage until click position

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
8 2030
"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: cassandra.flowers | last post by:
Hi, I have another string handling question for the group, since you have all been so helpful in the past. Thank you. Basically, I want...
0
by: mikelostcause | last post by:
Is there anyway to hold the base.WndProc(ref m) until after the Logout() finishes loading a webpage?? I'm working on shutting down an app that...
6
by: gk245 | last post by:
Basically, i want to make a function that will receive a sentence or phrase, and count its words. It would start like this (i think): #include...
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: ...
5
by: Jason | last post by:
I have a div box with a border, and some padding left and right. It has some text with different font sizes, and a few icons. I want to cut off any...
3
by: crazystone82 | last post by:
Hi friends, i need to drag a file from desktop and drop into web page.actually i had did a JS for dragging the images around the web page,but i...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file...
3
by: chandan | last post by:
Hi, Is any way to palce a control(button/label) at a location on webPage in runtime?? In page_load envet I am adding a button on the page in the...
17
by: Razii | last post by:
This is specifically regarding U++ which is C++ libraries and IDE (please don't whine whether its on or off topic. Right click on the thread and...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.