Tech Note: A Bookmarklet to Count Selected Characters and Words on a Web Page

Break it apart and count the pieces
LGF • Views: 27,156

Here, have a bookmarklet that counts characters and words in selected text on a web page:

javascript:(function(){var a=(document.selection?document.selection.createRange().text:document.getSelection()).toString();alert(a.length?"Characters: "+a.length+"\nWords: "+a.replace(/\s{2,}/g," ").split(" ").length:"No text selected.")})();

I bashed this out today because I wanted to easily be able to see how many words were in a given article; I used to copy and paste the article into a new BBEdit window and see the count there, but this is much simpler - just select the text and click the bookmarklet.

You can just select this code, then drag it to your browser’s bookmarks bar and give it a title. Or you can drag this nice button to the bookmarks bar instead and the title will already be filled in:

Here’s the code in a more readable form with indentation, before being minified.

It’s very simple, actually; just gets the selected text (in a way that’s compatible with IE or everything else), then puts up an alert box showing the count. The only tricky part is the regular expression in line 10 — it replaces all runs of more than one whitespace character with just one space, so when it splits the text by spaces it doesn’t end up with blank words in the array.

(You can’t drag the following code to the bookmarks bar - use the button above for that.)

(function() {
	var selected = (
		document.selection
		? document.selection.createRange().text
		: document.getSelection()
	).toString();

	alert(
		selected.length
		? 'Characters: ' + selected.length + '\nWords: ' + selected.replace(/\s{2,}/g,' ').split(' ').length
		: 'No text selected.'
	);
}());

It could be refined some more — right now it won’t count selected text that’s inside a frame or iframe, just in the main body of a page.

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2023-04-04 11:11 am PDT
LGF User's Guide RSS Feeds

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Cash.app
Recent PagesClick to refresh
Why Did More Than 1,000 People Die After Police Subdued Them With Force That Isn’t Meant to Kill? An investigation led by The Associated Press has found that, over a decade, more than 1,000 people died after police subdued them through physical holds, stun guns, body blows and other force not intended to be lethal. More: Why ...
Cheechako
3 hours ago
Views: 30 • Comments: 0 • Rating: 0
A Closer Look at the Eastman State Bar DecisionTaking a few minutes away from work things to read through the Eastman decision. As I'm sure many of you know, Eastman was my law school con law professor. I knew him pretty well because I was also running in ...
KGxvi
7 hours ago
Views: 84 • Comments: 1 • Rating: 1