How to find all words on a page, and how many of each word there is
<p>I am looking for a way to identify all the words on a page, and count how many of each instance of each word there is on that page. I need to use JavaScript for this though, and no jQuery.</p>

<p><strong>UPDATE</strong></p>

<p>This is what i have so far, although it seems to be working, i am still getting some cases where 2 or more words have been merged together, any clues?</p>

<pre><code>if(window.attachEvent) {
    window.attachEvent("onload", myFunc);
} else {
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            myFunc();
        };
        window.onload = newonload;
    } else {
        window.onload = myFunc;
    }
}

function myFunc() {
    var words = document.body.innerText;  
    words = words.replace(/\n/g, " "); //Remove line breaks
    words = words.split(" ");
    var foundWords = new Array(); 
    var counts = new Array(); 
    words.forEach(function(s) { 
        s = s.replace(/^\s+|\s+$/g,''); //Trim
        s = s.toLowerCase(); //To lower case
        var index = foundWords.indexOf(s);
        if(s != \'\') { //If word not blank
            if(index &lt; 0) {
                foundWords.push(s);
                var newindex = foundWords.indexOf(s);
                counts.push(1);
            } else {
                counts[index] += 1; 
            }
        }

    });

    //Cycle through all found words and log the index, word &amp; count
    foundWords.forEach( function(s) { 
        var index = foundWords.indexOf(s);
        console.log(index+" "+s+" "+counts[index]);
    });
}
</code></pre>
