This may not be strictly related to online marketing, but in the course of my work, I often have to count the number of words and characters in a given text (like page titles and descriptions). If you’ve ever used Microsoft Word, you know how that works.

Well, before Firefox 4.0, there was a nice add-on to do it, but it’s not compatible with the new version (how annoying). So I’ve had to resort to desperate measures.

Just kidding. Firefox allows the creation of JavaScript bookmarks, so you can put anything into them, really, and perform useful tasks like counting words and characters with a click of a button. In fact, Chrome provides the same mechanism, so this works on both browsers.

Thanks to as post by Joe Maller and another one by Media College, I had everything I needed. The code is this:

javascript:d=window.getSelection()+'';%20d=(d.length==0)?document.title:d;%20alert(d.split('%20').length+' words, '+d.length+' characters');

To install this “button”, drag the WordCount link to your Firefox or Chrome bookmarks area and you’re done. Just select some text, click the “button” (you can test now by clicking the link above) and a message will pop up with those numbers for you.

Note: when selecting text, make sure there are no trailing spaces or you will get an extra word and, of course, an extra character (this may happen when you double-click or triple-click).

Enjoy,
Gal

Edit (13/02/2010): To select text within iframes, try this code, posted by Thelizardreborn:

javascript:d=window.getSelection()+'';i=0;while(d=='null'||(d.length==0&&i<window.frames.length)){d=window.frames[i].getSelection()+'';i++;}d=(d.length==0)?document.title:d;alert(d.trim().split('%20').length+'%20words,%20'+d.length+'%20characters');

Or drag this WordCount link to your bookmark bar.


20 responses to “Word Count for Firefox & Chrome”

  1. I have been looking for this ever since WordCount-Plus became incompatible
    with FFox5. Thank you so much, Mr. Baras. Come to Berlin, and we will treat you for lunch.

  2. This is a great idea which works fine on some web pages, but not on others. With Firefox 6 and 7, there are many web pages in which the Javascripts fails to count the selected text. For example, it fails to properly count text in email messages displayed by Gmail or Yahoo email. For some reason, the Javascript you’ve offered always ends up with some text other than what was selected. Any thoughts of how to make it work in pages such as Gmail and Yahoo email?

    • I suspect both of these sites use massive amounts of AJAX that are well beyond my control. If any of their code changes the value of window.getSelection(), which is likely, there’s really nothing you or I can do in JavaScript to stop it without breaking the functionality of the page.

      Also, this snippet was something I developed for my personal use when I was using FF4. Since then, I’ve switched to Chrome and have no plans to go back.

      • Thanks for the reply Gal.

        Based on some experiments, it appears that the web sites on which your script fails are all sites which use iframes. I think that the basic idea of your script could be extended to include iframes… but I don’t know yet if it’s worth the effort. I’ll tinker with the idea of scanning all of the document’s iframes for selected text.

      • I’m having the same problem trying to use the link in the Blackboard-based online courseroom I teach in (which is why I installed it).  I’m in FF 3.6.23 because Blackboard doesn’t support any of the FF updates….  I’d love to know if the script gets updated because that’s the one place I really need it, so I can do quick checks on how long student discussion posts are.

        • Unfortunately, I can’t tackle every possible scenario and I’m not a JavaScript expert. You can try and post a description of the code structure (iframes, flash objects or whatever else is involved) and maybe someone (even me) will have an idea.

          • This modification should take one level of frames into account. If text is selected in multiple frames (or in a frame(s) and the main document) it will only count the first selection it comes to.

            javascript:d=window.getSelection()+”;i=0;while(d==’null’||(d.length==0&&i<window.frames.length)){d=window.frames[i].getSelection()+'';i++;}d=(d.length==0)?document.title:d;alert(d.trim().split('%20').length+'%20words,%20'+d.length+'%20characters');

  3. awesome add-on, most of the firefox add-ons were not working for me…this one isn’t a addon, but is good anyway. I wanted this coz i enter lots of competitions (25 words or less…), so it comes in handy if the page doesn;t provide a counter

    Thanks a bunch

  4. It’s difficult to find out
    the word count in the Firefox browser though its good in chrome. it does not
    seem unreasonable to suggest that every single guided process flows are very
    much productive as well as effective in terms of précising the current
    situation of actual word counts in the progression in all the way. Therefore
    every one should guide to make the best uses in chrome other than Firefox using
    process.

    • @lopi, I hope I understand you correctly. Firefox has changed a lot since the publication of this post and this may have affected the word count function. On Chrome, the code still works well and I offer no guarantees or maintenance of it.

      See Harry’s alternative suggestions.

Leave a Reply to Hunter Vn 960 Cancel reply

Your email address will not be published. Required fields are marked *