Guest User

Untitled

a guest
Jun 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. <div id="content">
  2. hello how are you?
  3. </div>
  4.  
  5. var count = document.getElementById('content').innerHTML.split(' ').length;
  6.  
  7. function get_text(el) {
  8. ret = "";
  9. var length = el.childNodes.length;
  10. for(var i = 0; i < length; i++) {
  11. var node = el.childNodes[i];
  12. if(node.nodeType != 8) {
  13. ret += node.nodeType != 1 ? node.nodeValue : get_text(node);
  14. }
  15. }
  16. return ret;
  17. }
  18. var words = get_text(document.getElementById('content'));
  19. var count = words.split(' ').length;
  20.  
  21. var count = words.split(/s+/).length;
  22.  
  23. document.deepText= function(hoo){
  24. var A= [];
  25. if(hoo){
  26. hoo= hoo.firstChild;
  27. while(hoo!= null){
  28. if(hoo.nodeType== 3){
  29. A[A.length]= hoo.data;
  30. }
  31. else A= A.concat(arguments.callee(hoo));
  32. hoo= hoo.nextSibling;
  33. }
  34. }
  35. return A;
  36. }
  37.  
  38. function countwords(hoo){
  39. var text= document.deepText(hoo).join(' ');
  40. return text.match(/[A-Za-z'-]+/g).length;
  41. }
  42. alert(countwords(document.body))
  43.  
  44. var count = !s ? 0 : (s.split(/^s+$/).length === 2 ? 0 : 2 +
  45. s.split(/s+/).length - s.split(/^s+/).length - s.split(/s+$/).length);
  46.  
  47. function CountWords (this_field, show_word_count, show_char_count) {
  48. if (show_word_count == null) {
  49. show_word_count = true;
  50. }
  51. if (show_char_count == null) {
  52. show_char_count = false;
  53. }
  54. var char_count = this_field.value.length;
  55. var fullStr = this_field.value + " ";
  56. var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
  57. var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
  58. var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
  59. var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
  60. var splitString = cleanedStr.split(" ");
  61. var word_count = splitString.length -1;
  62. if (fullStr.length <2) {
  63. word_count = 0;
  64. }
  65. if (word_count == 1) {
  66. wordOrWords = " word";
  67. } else {
  68. wordOrWords = " words";
  69. }
  70. if (char_count == 1) {
  71. charOrChars = " character";
  72. } else {
  73. charOrChars = " characters";
  74. }
  75. if (show_word_count & show_char_count) {
  76. alert ("Word Count:n" + " " + word_count + wordOrWords + "n" + " " + char_count + charOrChars);
  77. } else {
  78. if (show_word_count) {
  79. alert ("Word Count: " + word_count + wordOrWords);
  80. } else {
  81. if (show_char_count) {
  82. alert ("Character Count: " + char_count + charOrChars);
  83. }
  84. }
  85. }
  86. return word_count;
  87. }
Add Comment
Please, Sign In to add comment