Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Get the Word count for page

Shetty119
Level 2
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
Rajashankar
Correct answer by
Level 4
Level 4

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
Umesh_Thakur
Community Advisor
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

Rajashankar
Correct answer by
Level 4
Level 4

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

Shetty119
Level 2
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.