It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Javascript - Word Count - Help!!

Javascript - Word Count - Help!!

Postby Deepu on Sun Aug 07, 2011 11:09 pm

Hi Guys,

I have a javascript, which counts the specified words..

I want javascript to count some of the html tags and symbols when i enter a html code in the textarea..

- the below searchwords are not functioning properly.
http://(space)
http:/(space)
etc... (no spaces are searching)

- www. (.(dot) is not searching, also when i enter wwww(4 times) in the textarea it shows www. as count 1)

- i can't enter the below symbols in searchwords array
[
<
'
?

Please help me out guys...

Here is my Code...

[code]
<html>
<head>
<title>Word Count</title>
<script type = "text/javascript">
function searchForWords(){
document.getElementById('resultsContainer').innerHTML = '';
var txt = document.getElementById('txtMsg').value;
var strWords = document.getElementById('txtSearchWords').value;
var searchWordsTokens = strWords.split(' ');
var searchWords = ['href','LINK_ID','mailto','src="','src= ','src =','.jpg','.gif','.png','www','www.','http','http:// ','http:/ ','http: ','http ',' http',' "','@','amp;','TBD'];
for(i=0; i < searchWordsTokens.length; i++){
if(searchWordsTokens[i].length > 0){searchWords.push(searchWordsTokens[i]);} //remove spaces between words
}
var results = [],matches;
for(i=0; i < searchWords.length; i++){
var regex = new RegExp('\\b'+searchWords[i]+'\\b','gi');
matches = txt.match(regex);
results[searchWords[i]] = (matches)? matches.length : 0;
}
//output results
var str = '';
for(var i in results){
str += i+'--'+results[i]+'<br />';
}
document.getElementById('resultsContainer').innerHTML = str;
}
window.onload=function(){
document.getElementById('form_submit').onclick=searchForWords;
}
</script>
</head>
<body>
<table>
<tr><td align="center">
<form id="contact_form"><h4>Paste your HTML Code here:</h4>
<p><label class="form_label" for='message'></label>
<textarea rows="20" cols="60" id="txtMsg"></textarea>
</p>
<div>
<input type="text" id="txtSearchWords"/>
</div>
<p><input id='form_submit' type="button" value="Check for Counts"></p>
<div><p><input id='form_submit' type="reset" value="Clear"></p></div>
<div id="resultsContainer"></div>
</form><br /></td>
</table>
</body>
</html>
[/code]
Deepu
 
Posts: 1
Joined: Sun Aug 07, 2011 11:04 pm

Re: Javascript - Word Count - Help!!

Postby rajmv on Wed Aug 10, 2011 8:03 pm

I think this will help you in the right direction:

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Word Count</title>
<script type = "text/javascript">
function searchForWords(){
document.getElementById('resultsContainer').innerHTML = '';
var txt = document.getElementById('txtMsg').value;
var strWords = document.getElementById('txtSearchWords').value;
var searchWords = [/href\s*=/gi,/LINK_ID/gi,/mailto\s*:/gi,/src\s*=/gi,/\.jpg/gi,/\.gif/gi,/\.png/gi,/id\s*=/gi];
var str = '';
for (var i=0; i<searchWords.length; i++) {
   var matches = txt.match (searchWords[i]);
   var sw = '' + searchWords[i];
   sw = sw.replace('/gi','');
   sw = sw.substr(1);
   str += sw + ' : ' + (matches?matches.length:0) + '<br/>';
}


document.getElementById('resultsContainer').innerHTML = str;
}
window.onload=function(){
document.getElementById('form_submit').onclick=searchForWords;
}
</script>
</head>
<body>
<table>
<tr><td align="center">
<form id="contact_form"><h4>Paste your HTML Code here:</h4>
<p><label class="form_label" for='message'></label>
<textarea rows="20" cols="60" id="txtMsg"></textarea>
</p>
<div>
<input type="text" id="txtSearchWords"/>
</div>
<p><input id='form_submit' type="button" value="Check for Counts"></p>
<div><p><input id='form_submit' type="reset" value="Clear"></p></div>
<div id="resultsContainer"></div>
</form><br /></td>
</table>
</body>
</html>
rajmv
100+ Club
 
Posts: 103
Joined: Thu Jul 14, 2011 7:22 am


Who is online

Users browsing this forum: No registered users and 9 guests