Word Count for Firefox & Chrome

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.

Categories
  • Juan Lapeyre

    Excellent work! I need this function. Great job!
    Thanks from Lima, Perú

  • http://www.zeit.de Wolfgang Blau

    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.

    • http://get-business-online.com/ Gal Baras

      OK, I’ll just go get me a flight, then :)

      Glad you liked it. I use it often.

  • http://seox.me/ SEOx

    Great! I wasn’t updating my ffx until i read this work around :)

  • james

    doens’t work in google sites with hebrew

    • http://get-business-online.com/ Gal Baras

      Give me an example, because it certainly works on Hebrew content on Walla.

  • JP

    Great tool – thanks! Will share and spread the love.

  • rjs

    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?

    • http://get-business-online.com/ Gal Baras

      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.

      • rjs

        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.

      • SA

        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.

        • http://get-business-online.com/ Gal Baras

          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.

          • thelr

            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');

  • Dadsf

    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

  • Hunter Vn 960

    It’s really verry good! Thank a lot!

  • David

    Thanks: really needed this.