Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:30:17+00:00 2026-05-23T19:30:17+00:00

My goal is to count all word in html page as well as count

  • 0

My goal is to count all word in html page as well as count fixed word in html page the prob is that using that function script tag text also get in count so how i remove script tag from counting keywords.
i this code MSO_ContentTable is id 0f div tag. give me any other solution on jquery also if there.

function CountWord(keyword) {

    var word = keyword.toUpperCase(),
        total = 0,
        queue = [document.getElementById('MSO_ContentTable')],
        curr, count = 0;

    while (curr = queue.pop()) {
        var check = curr.textContent;

        if (check != undefined) {

            for (var i = 0; i < curr.childNodes.length; ++i) {

                if (curr.childNodes[i].nodeName == "SCRIPT") {
                    // do nothing
                }
                else {
                    switch (curr.childNodes[i].nodeType) {
                        case 3: // 3
                            var myword = curr.childNodes[i].textContent.split(" ");

                            for (var k = 0; k < myword.length; k++) {
                                var upper = myword[k].toUpperCase();

                                if (upper.match(word)) {
                                    count++;
                                    wc++;
                                }
                                else  if((upper[0] >= 'A' && upper[0] <= 'Z') ||
                                         (upper[0] >= 'a' && upper[0] <= 'z') ||
                                         (upper[0] >= '0' && upper[0] <= '9')) {
                                    wc++
                                }                                    
                            }
                        case 1: // 1
                            queue.push(curr.childNodes[i]);
                    }
                }
            }
      }
}

thx
other problem is how i remove the tag which have their display property none?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:30:19+00:00Added an answer on May 23, 2026 at 7:30 pm

    In your code:

    > queue = [document.getElementById('MSO_ContentTable')],
    > curr, count = 0;
    > 
    > while (curr = queue.pop()) {
    

    getElementById will only ever return a single node, so no need to put it in an array and no need to pop it later:

    curr = document.getElementById('MSO_ContentTable');
    if (curr) {
      // do stuff
    

    .

    >    var check = curr.textContent;
    

    The DOM 3 Core textContent property is not supported by all browsers, you need to offer an alternative such as innerText, e.g.:

    // Get the text within an element
    // Doesn't do any normalising, returns a string
    // of text as found.
    function getTextRecursive(element) {
      var text = [];
      var self = arguments.callee;
      var el, els = element.childNodes;
    
      for (var i=0, iLen=els.length; i<iLen; i++) {
        el = els[i];
    
        // May need to add other node types here
        // Exclude script element content
        if (el.nodeType == 1 && el.tagName && el.tagName.toLowerCase() != 'script') {
          text.push(self(el));
    
        // If working with XML, add nodeType 4 to get text from CDATA nodes
        } else if (el.nodeType == 3) {
    
          // Deal with extra whitespace and returns in text here.
          text.push(el.data);
        }
      }
      return text.join('');
    }
    

    .

    >    if (check != undefined) {
    

    Given that check will always be a string (even if textContent or innerText are used instead of the above function), testing against undefined doesn’t seem appropriate. Also, I don’t understand why this test is done before looping over the child nodes.

    Anyhow, the getText function above will return the text content without script elements, so you can just use that to get the text then play with it as you want. You may need to normalise whitespace as different browsers will return different amounts.

    PS. I should note that arguments.callee is restricted in ES5 strict mode, so if yo plan on using strict mode, replace that expression with an explicit call to the function.

    Edit

    To exclude not visible elements, you need to test each one to see if it’s visible. Only test elements, don’t test text nodes as if their parent element is not visible, the text won’t be.

    Note that the following is not widely tested yet, but works in IE 6 and recent Firefox, Opera and Chrome at least. Please test thoroughly before using more widely.

      // The following is mostly from "myLibrary"
      // <http://www.cinsoft.net/mylib.html>
      function getElementDocument(el) {
        if (el.ownerDocument) {
          return el.ownerDocument;
        }
        if (el.parentNode) {
          while (el.parentNode) {
            el = el.parentNode;
          }
          if (el.nodeType == 9 || (!el.nodeType && !el.tagName)) {
            return el;
          }
    
          if (el.document && typeof el.tagName == 'string') {
            return el.document;
          }
          return null;
        }
      }
    
    
      // Return true if element is visible, otherwise false
      //    
      // Parts borrowed from "myLibrary"
      // <http://www.cinsoft.net/mylib.html>
      function isVisible(el) {
        if (typeof el == 'string') el = document.getElementById(el);
    
        var doc = getElementDocument(el);
        var reVis = /\bhidden\b|\bnone\b/;
        var styleObj, isVis; 
    
        // DOM compatible
        if (doc && doc.defaultView && doc.defaultView.getComputedStyle) {
          styleObj = doc.defaultView.getComputedStyle(el, null);
    
        // MS compatible
        } else if (el.currentStyle) {
          styleObj = el.currentStyle;
        }
    
        // If  either visibility == hidden || display == none
        // then element is not visible
        return !reVis.test(styleObj.visibility + ' ' + styleObj.display);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The goal is that I would like to cycle through ALL of the .CSV
Goal: to find count of all words in a file. file contains 1000+ words
Goal is to make a dialog that appears on menu_key pressed, but it keeps
Goal: Produce an Excel document with information from 3 associated models that is similar
I wrote an extension to count the product sales - goal is to get
Trying to get a simple COUNT from a table that takes a couple of
My goal is to have dynamic Facebook like buttons using a php variable in
Ok guys, today's goal is to build a Turing machine simulator. For those that
Goal: Get all panels whose due date is <= today's date. Entity/db diagram :
My goal is to trim all string datatypes within my dataset and then replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.