473,728 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2132
"regrat" <it********@yah oo.it> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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.ht m</title>
<script type="text/javascript">
function words() {
var oHTM = new ActiveXObject(" htmlfile")
var sTXT = oHTM.parentWind ow.clipboardDat a.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.onclic k to listen for mouse

2. onclick - Capture the the TextRange object using
document.select ion.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.sp lit(/\b\w+\b/g);

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

Sep 22 '05 #3
JRS: In article <Ku************ ********@comcas t.com>, dated Thu, 22 Sep
2005 10:05:07, seen in news:comp.lang. javascript, McKirahan
<Ne**@McKirahan .com> posted :
"regrat" <it********@yah oo.it> wrote in message
news:11******* *************** @g14g2000cwa.go oglegroups.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.parentWind ow.clipboardDat a.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.i nnerText.split(/\s+/).length)

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of 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.
Sep 22 '05 #4
Dr John Stockton said the following on 9/22/2005 4:36 PM:
JRS: In article <Ku************ ********@comcas t.com>, dated Thu, 22 Sep
2005 10:05:07, seen in news:comp.lang. javascript, McKirahan
<Ne**@McKirahan .com> posted :
"regrat" <it********@yah oo.it> wrote in message
news:11****** *************** *@g14g2000cwa.g ooglegroups.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.i nnerText.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.javas cript 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.getSe lection)
txt = document.getSel ection();
else if (document.selec tion)
txt = document.select ion.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************ ********@comcas t.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.i nnerText.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.demo n.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demo n.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************ ********@comcas t.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.i nnerText.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.javas cript 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
6726
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 to do something really simple: Search a main string for a substring, then count how many times the
0
1139
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 runs in the system tray, I have no problems shutting down, but I have problems saving data first. if the base.WndProc(ref m) is placed at the top, it closes, but the system does not continue to shutdown and it does not save the data via the Logout() funtion. If the base.WndProc(ref m) is placed...
6
1872
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 <stdio.h> int count ( char sentence ) { char string ;
3
6353
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: Words are delimited by white spaces (blanks, tabs, linefeeds, returns). It will print 3 pieces of information before exit: the first word, the last word, and the number of words read, with one blank in between fields. Input lines of words Output
5
6410
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 overflow at the end of the line with "...", instead of the text wrapping. What is the best way I can detect whether the line wrapped, and how many characters to cut off at the end? - Jason
3
11860
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 dont know how to drag the file and drop it into web page.the js code for dragging around webpage is given below <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- Original: Randy Bennett...
7
2113
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 and create a new text file that contans those 27 lines. and after that save both of those files... since that is a big block of text (15000 lines) i thint that it is a big job to look for a keyword... so my question is this exactly: how do i do this:
3
1553
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 contol collection of a panel but not able to specify the location of the button since button controls did not have location property in web. I am trying to place a control at mouse click position on the page.also I am not getting the mouse down position on the page I am
17
1620
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 click on ignore instead of wasting time). This is specifically for Mirek Fidler. I am on a new computer (with Vista instead of XP, 3 gig ram, and 3 core phenom). I downloaded U++ IDE (with mingw). Compiled the counting words benchmark (with MINGW speed option) and it was much slower than before...
0
8903
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8758
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9422
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9198
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8129
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6716
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4792
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2161
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.