tinyMCE Word Count

[Edit July 13, 2008]: If you are using a version that is older than 3.x then replace

var strip=stripcode(tinyMCE.activeEditor.getContent())

with this:

var strip=stripcode(tinyMCE.getContent(tiny_ID));

[/Edit]

This script will add a word count to your tinyMCE text editor.

First, put this in your header, or if that isn't possible for whatever reason, put it above your tinyMCE code, while editing the part the variable that is your tinyMCE content ID.

<script language="javascript" type="text/javascript">
function stripcode(oldstring)
{
var newstring = "";
var tag = false;
for(var x = 0; x < oldstring.length; x++)
{
if(oldstring.charAt(x) == '<')
{
tag = true;
}
if(oldstring.charAt(x) == '>')
{
tag = false;
x++;
}
if(tag==false)
{
newstring += oldstring.charAt(x);
}
}
return newstring;
}

function countWords()
{
var tiny_ID; //This is the ID of your tinyMCE content ID
var strip=stripcode(tinyMCE.activeEditor.getContent())
strip=stripcode(strip);

if(strip=="")
{
document.myform.wordcount.value=0;
}
else
{
document.myform.wordcount.value = strip.split(' ').length;
}
}

</script>



Next what you are going to do is after the tinyMCE code, you are going to put this code:

<input type="button" value="Calculate Words"
onClick="countWords()" /><input type="text" name="wordcount" size="20" />


Twitter








Link Directory | link directory | GeekySpeaky: Submit Your Site! | Blog Matter

Copyright © 2004-2017 Sean Noble, All rights reserved
Read our Disclosure Policy