Get the Word count for page | Community
Skip to main content
Level 2
August 6, 2021
Solved

Get the Word count for page

  • August 6, 2021
  • 2 replies
  • 1997 views

Hi Team,

 

Please suggest me how I can get the word count for a page. We do customisations to the AEM tool.

Eg: Page authored has many components . So it should count all the words present on page.

 

Also is it possible to customise the rte word count plugin to count the words on page.

 

Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by RajaShankar

Hi @shetty119 

My two cents place a class identifier at start and end of page body from where you want to count the words. Now Use this JS once your DOM load is complete for page.

 

var wordsInPost = wordCount(document.getElementsByClassName(".custom-class"))

function wordCount(words) { var count = 0 for (var i = 0; i < words.length; i++) { count += words[i].textContent.split(" ").length } return count }

console.log(wordsInPost)

 

Refer this Article:

https://techstacker.com/vanilla-javascript-count-all-words-webpage/

 

Regards,

Rajashankar

2 replies

Umesh_Thakur
Community Advisor
Community Advisor
August 6, 2021

there can be multiple way of doing this either with the help of custom javascript code like 

 

<script>      function countWords(str) {         str = str.replace(/(^\s*)|(\s*$)/gi,"");         str = str.replace(/[ ]{2,}/gi," ");         str = str.replace(/\n /,"\n");         return str.split(' ').length;      }      document.write(countWords("there can be multiple way of doing this either   with the help of custom javascript code like "));   </script>

 or take help from browser plug-ins for the same purpose like Word Count of google chorme.

Hope this will help.

Umesh Thakur

RajaShankar
Community Advisor
RajaShankarCommunity AdvisorAccepted solution
Community Advisor
August 6, 2021

Hi @shetty119 

My two cents place a class identifier at start and end of page body from where you want to count the words. Now Use this JS once your DOM load is complete for page.

 

var wordsInPost = wordCount(document.getElementsByClassName(".custom-class"))

function wordCount(words) { var count = 0 for (var i = 0; i < words.length; i++) { count += words[i].textContent.split(" ").length } return count }

console.log(wordsInPost)

 

Refer this Article:

https://techstacker.com/vanilla-javascript-count-all-words-webpage/

 

Regards,

Rajashankar

Shetty119Author
Level 2
August 8, 2021

Hi Rajashankar,

 

Thanks for the reply.

The above code which you have suggested is working fine in Google but not in safari. Please suggest for Safari as well.

Also is there a way to capture the live count of the page?

 

Thanks.