Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question can be split into two parts:</p> <ol> <li>How can we get the the webpage text content without HTML tags?</li> </ol> <p>We can generalize the second question a bit.</p> <ol start="2"> <li>How can we find the number of string occurrences in another string?</li> </ol> <p>And the 'best possible way to do this':</p> <p>Amaan got the idea right of finding the text, but lets take it further.</p> <pre><code>var text = document.body.innerText || document.body.textContent; </code></pre> <p>Adding <code>textContent</code> to the code helps us cover more browsers, since <code>innerText</code> is not supported by all of them.</p> <p>The second part is a bit trickier. It all depends on the number of '$' symbol occurrences on the page.</p> <p>For example, if we know for sure, that there is at least one occurrence of the symbol on the page we would use this code:</p> <pre><code>text.match(/\$/g).length; </code></pre> <p>Which performs a global regular expression match on the given string and counts the length of the returned array. It's pretty fast and concise.</p> <p>On the other hand, if we're not sure if the symbol appears on the page at least once, we should modify the code to look like this:</p> <pre><code>if (match = text.match(/\$/g)) { match.length; } </code></pre> <p>This just checks the value returned by the match function and if it's null, does nothing.</p> <p>I would recommend using the third option only when there is a large occurrence of the symbols in the page or you're going to perform the search many many times. This is a custom function (taken from <a href="https://stackoverflow.com/a/7924240/691148">here</a>) to count the occurrence of the specified string in another string. It performs better than the other two, but is longer and harder to understand.</p> <pre><code>var occurrences = function(string, subString, allowOverlapping) { string += ""; subString += ""; if (subString.length &lt;= 0) return string.length + 1; var n = 0, pos = 0; var step = (allowOverlapping) ? (1) : (subString.length); while (true) { pos = string.indexOf(subString, pos); if (pos &gt;= 0) { n++; pos += step; } else break; } return (n); }; occurrences(text, '$'); </code></pre> <p>I'm also including a little jsfiddle <a href="http://jsfiddle.net/qgnYT/" rel="nofollow noreferrer">'benchmark'</a> so you can compare these three different approaches yourself.</p> <p>Also: No, there isn't a better way of doing this than just getting the body text and counting how many '$' symbols there are.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload