Count Characters, Words and Pages with a simple bookmarket

posted 03:11PM Jan 07, 2008 with tags bookmarklet javascript tips by Lars Trieloff

If you are writing texts online, you may come across forms that require a specific minimum or maximum number of pages, words or characters in a text field or textarea. The Count Words bookmarklet is designed to help you with meeting this requirement. As with all bookmarks, all you have to do is pulling the bookmarklet into your bookmarks bar, when you need it, click the bookmarklet and it will display how many characters, words and pages you have written for each individual text field or textarea.

For the curious: I've used Dead Edward's Packer to compress the bookmarklet, the original source code looks like this:

function f() {
  var D=document,i,f,j,e;
  
  function S(e) {
    if(!e.N) {
      var x=D.createElement("span");
      var s=x.style;
      s.color="#333333";
      s.border="1px solid #aaaaaa";
      s.background="#cccccc";
      s.font="10pt sans-serif";
      s.verticalAlign="top";
      //s.float="left";
      //s.clear="left";
      e.parentNode.insertBefore(x,e.nextSibling);
      function u() {
        x.innerHTML=e.value.split(" ").length + " words, " + e.value.length + " 
characters, " + (Math.floor(e.value.length / (1800 / 2))/2) + " pages";
      }
      u();
      e.onchange=u;
      e.onkeyup=u;
      e.oninput=u;
      e.N=x;
    } else {
      e.parentNode.removeChild(e.N);
      e.N=0;
    }
  }
  
  for(i=0;f=D.forms[i];++i) {
    for(j=0;e=f[j];++j)
      if(e.type=="text"||e.type=="password"||e.tagName.toLowerCase()=="textarea"
)
        S(e);
  }
}

f();