Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Get the Word count for page

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 2

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.