Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <div id="content">
- hello how are you?
- </div>
- var count = document.getElementById('content').innerHTML.split(' ').length;
- function get_text(el) {
- ret = "";
- var length = el.childNodes.length;
- for(var i = 0; i < length; i++) {
- var node = el.childNodes[i];
- if(node.nodeType != 8) {
- ret += node.nodeType != 1 ? node.nodeValue : get_text(node);
- }
- }
- return ret;
- }
- var words = get_text(document.getElementById('content'));
- var count = words.split(' ').length;
- var count = words.split(/s+/).length;
- document.deepText= function(hoo){
- var A= [];
- if(hoo){
- hoo= hoo.firstChild;
- while(hoo!= null){
- if(hoo.nodeType== 3){
- A[A.length]= hoo.data;
- }
- else A= A.concat(arguments.callee(hoo));
- hoo= hoo.nextSibling;
- }
- }
- return A;
- }
- function countwords(hoo){
- var text= document.deepText(hoo).join(' ');
- return text.match(/[A-Za-z'-]+/g).length;
- }
- alert(countwords(document.body))
- var count = !s ? 0 : (s.split(/^s+$/).length === 2 ? 0 : 2 +
- s.split(/s+/).length - s.split(/^s+/).length - s.split(/s+$/).length);
- function CountWords (this_field, show_word_count, show_char_count) {
- if (show_word_count == null) {
- show_word_count = true;
- }
- if (show_char_count == null) {
- show_char_count = false;
- }
- var char_count = this_field.value.length;
- var fullStr = this_field.value + " ";
- var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
- var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
- var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
- var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
- var splitString = cleanedStr.split(" ");
- var word_count = splitString.length -1;
- if (fullStr.length <2) {
- word_count = 0;
- }
- if (word_count == 1) {
- wordOrWords = " word";
- } else {
- wordOrWords = " words";
- }
- if (char_count == 1) {
- charOrChars = " character";
- } else {
- charOrChars = " characters";
- }
- if (show_word_count & show_char_count) {
- alert ("Word Count:n" + " " + word_count + wordOrWords + "n" + " " + char_count + charOrChars);
- } else {
- if (show_word_count) {
- alert ("Word Count: " + word_count + wordOrWords);
- } else {
- if (show_char_count) {
- alert ("Character Count: " + char_count + charOrChars);
- }
- }
- }
- return word_count;
- }
Add Comment
Please, Sign In to add comment