views:

391

answers:

2

I want to count number of words & number of characters in html text (TEMP),

and while doing that i want to skip HTML tags and html keywords (ex.  )

suppose i am getting HTML text in my JavaScript function as

var TEMP= result.data;  

// where result.data='<p>BODY&nbsp;Article By Archie(Used By Story Tool)</p>';

i have done this:

var e = document.createElement("span");
        e.innerHTML = TEMP;
        var text = e.innerText;
        var characterCount = text.length;                    
        var wordCount = text.match(/\b\w/g).length;

works in Internet Explorer & Chrome,

but this code doesn't work in mozilla firefox as above code has .innerText property which is not supported by FireFox so i have also tried o.k.w' s code but didn't work in mozilla

o.k.w's code

function getCharCount(str){
    var d = document.createElement("div");
    d.innerHTML = str;
    if(d.innerText) //for IE and others
        alert(d.innerText.length);
    else //for FF
        alert(d.textContent.length);
}

pls help

and important this should work on all (IE,Chrome,mozilla firefox )browser.

can we detect browser (ch, IE, FF)and change code accordingly.?

is there any other way to count words and characters to avoid this issue??

+1  A: 

Hai elcon ,

You can use "textContent" in mozilla which is equivalent to "innerText" in IE.

function getInnerText(o)
 {
   return o.textContent ? o.textContent : o.innerText;
 }
Pandiya Chendur
A: 

also is here related example

http://java.pakcarid.com/Cpp.aspx?sub=366&amp;ff=2955&amp;topid=34&amp;sls=25

lois