Keyword Density Analyzer is a tool that can be used to find the total number of keywords in a particular piece of text. Creating web pages with a good keyword density(between 4% to 8%) helps in achieving a good search engine ranking.
Just Copy and Paste the below code in a text editor and save it with any filename with extenstion .html and execute it in the browser.
<html>
<head>
<title>JavaScript Keyword Density Analyzer</title>
<script language="JavaScript">
function count(txt_element)
{
str=document.getElementById(txt_element).innerHTML;
var words = str.replace(/[^a-zA-Z0-9]/g,' ').split(' ');
var assoc = new Array();
total_words=words.length;
for (i=0; i<total_words; i++)
{
if (!words[i])
continue;
if (assoc[words[i]] == null)
assoc[words[i]]=0;
assoc[words[i]]++;
}
output = new Array();
for (word in assoc)
{
output[output.length]=word + ": " + assoc[word] + ", Density: " + Math.round((assoc[word]/total_words)*100) + "%";
}
output.sort();
document.getElementById('outputId').innerHTML="<b>Keyword Density</b><br/><i>Total Words:"+total_words+"</i><br/>"+output.join('<br/>');
}
</script>
</head>
<body>
<h2>JavaScript Keyword Density Analyzer</h2>
<textarea name="txt_text" id="txt_text" cols="50" rows="5"></textarea><br/><br/>
<input type="submit" name="Submit" value="Calculate Keyword Density" onClick="javascript: count('txt_text');">
<br/><br/>
<div id="outputId"></div>
</body>
</html>