Selection Word Count Bookmarklet

A zero-install browser tool that counts words in any selected text on any webpage. Drag it to your bookmarks bar once, use it everywhere—no extensions, no permissions, no server calls. Perfect for writers checking quotas, students verifying assignments, or anyone working with text limits.
Core Features
- One-click word count for selected text.
- Works on any webpage (Gmail, Google Docs, Wikipedia, blog editors).
- No installation: pure JavaScript bookmarklet.
- Instant alert display with count.
- Regex-based tokenization (handles punctuation, whitespace).
- Privacy-friendly: runs entirely in-browser, no data sent anywhere.
How to Install
- Show your bookmarks bar: Press
Ctrl+Shift+B(Chrome/Edge) orCmd+Shift+B(Mac). - Drag this link to your bookmarks bar:
<a href="javascript:(function()%7Balert(window.getSelection().toString().match(%2F%5Cw%2B%2Fg).length%20%2B%20%20%22%20Words%22)%7D)()">Word Counter</a> - Select text on any webpage.
- Click the bookmarklet in your bar.
- See the word count in an alert popup.
How It Works
Bookmarklets are JavaScript code stored in browser bookmarks. When clicked, they execute on the current page.
Encoded Bookmarklet
javascript:(function()%7Balert(window.getSelection().toString().match(%2F%5Cw%2B%2Fg).length%20%2B%20%20%22%20Words%22)%7D)()
Decoded JavaScript
(function() {
alert(window.getSelection().toString().match(/\w+/g).length + " Words")
})()
Step-by-step:
window.getSelection()— Grabs the user’s currently highlighted text..toString()— Converts the Selection object to a string..match(/\w+/g)— Matches all word characters (letters, digits, underscores) using regex..length— Counts the number of matched words.alert(...)— Displays the count in a browser popup.
Regex Pattern Explanation
/\w+/g
\w— Matches any word character (a-z, A-Z, 0-9, _).+— One or more consecutive word characters (forms a “word”).g— Global flag: find all matches, not just the first.
This simple pattern handles:
- Multiple spaces/tabs between words.
- Punctuation separating words.
- Line breaks.
Limitation: Contractions like “don’t” count as two words (don and t). For more sophisticated parsing, use \b\w+\b or library-based tokenization.
Use Cases
- Writers: Verify article meets 500-word minimum.
- Students: Check essay word count before submission.
- Email: Stay under character/word limits.
- Social media: Optimize post length for platform constraints.
- Translators: Estimate translation workload from source text.
- Editors: Quick sanity check on section lengths.
Alternatives & Extensions
Character Count Version
javascript:(function(){alert(window.getSelection().toString().length+" Characters")})()
Counts characters instead of words.
Word + Character Count
javascript:(function(){var t=window.getSelection().toString();alert(t.match(/\w+/g).length+" words, "+t.length+" chars")})()
Shows both counts in one alert.
Copy to Clipboard
javascript:(function(){var c=window.getSelection().toString().match(/\w+/g).length;navigator.clipboard.writeText(c);alert("Copied: "+c+" words")})()
Copies count to clipboard for pasting elsewhere.
Browser Compatibility
- ✅ Chrome, Edge, Firefox, Safari (all modern versions).
- ✅ Mobile browsers (iOS Safari, Chrome Android) — select text, tap bookmarklet from bookmarks menu.
- ⚠️ Some sites with strict Content Security Policy (CSP) may block bookmarklets.
- ⚠️ Doesn’t work inside iframes with different origins (cross-origin restrictions).
Troubleshooting
“Cannot read property ’length’ of null”
→ No text selected or regex returned no matches. Ensure text is highlighted before clicking.
Bookmarklet doesn’t appear to run
→ Check browser console for CSP errors. Some enterprise sites block inline JavaScript.
Count seems wrong
→ Regex treats hyphenated words or contractions as multiple tokens. For precision, use a dedicated word count tool.
Privacy & Security
✅ No data leaves your browser: All computation happens client-side.
✅ No permissions needed: Unlike extensions, bookmarklets don’t require install approval.
⚠️ Code is visible: Anyone can inspect the bookmark URL to see the logic (good for transparency).
Enhancement Ideas
- Display count in a styled overlay instead of alert (better UX).
- Exclude common stop words for “significant word” count.
- Track cumulative counts across multiple selections (session storage).
- Export counts to CSV for analysis.
- Integrate with writing goal tracking apps.
Summary
This one-liner bookmarklet delivers instant word counts without installation overhead—perfect for quick checks across diverse web environments. It’s a reminder that powerful tools don’t always need frameworks or backends; sometimes a well-crafted regex in 100 characters is all you need.