title: Count number of words in string using JavaScript
body: <p>I am trying to count the number of words in a given string using the following code:</p>
  
  <pre><code>var t = document.getElementById('MSO_ContentTable').textContent;
  
  if (t == undefined) {
    var total = document.getElementById('MSO_ContentTable').innerText;                
  } else {
    var total = document.getElementById('MSO_ContentTable').textContent;        
  }
  countTotal = cword(total);   
  
  function cword(w) {
    var count = 0;
    var words = w.split(" ");
    for (i = 0; i &lt; words.length; i++) {
      // inner loop -- do the count
      if (words[i] != "") {
        count += 1;
      }
    }
  
    return (count);
  }
  </code></pre>
  
  <p>In that code I am getting data from a div tag and sending it to the <code>cword()</code> function for counting. Though the return value is different in IE and Firefox. Is there any change required in the regular expression? One thing that I show that both browser send same string there is a problem inside the <code>cword()</code> function.</p>