Home: htmlArea 3 (beta): htmlArea 2 & 3 archive (read only): htmlArea v3.0 - Discussion:
count char - max char



Memby
New User

Jun 29, 2004, 5:26 AM


Views: 69828
count char - max char

I'd like to have a char counter for the version 3, and how can I limit the number of char.

Thanks. (soory for my english)


demuire
Novice

Oct 12, 2004, 7:42 PM


Views: 69693
Re: [Memby] count char - max char

Hmm, I'd like to know too. I saw the threads for the previous version, but can't figure out how to adapt it to fit v3?


demuire
Novice

Oct 13, 2004, 6:06 PM


Views: 69679
Re: [demuire] count char - max char

I've started working on one, I made a plugin called CharCounter. It's still very much work-in-progress, but the main thing in the plugin is this:


Quote
CharCounter.prototype.onKeyPress = function(editor, obj) {
updateCounter();
};


And then in the page calling I have this:


Quote
function updateCounter() {
var myText = editor.getHTML();
document.forms[0].counter.value = myText.length;

// alert("update the counter here!");
}


This still counts the HTML tags, but I think this can be fixed by including this code (from franknitty69 on [url=http://www.interactivetools.com/forum/gforum.cgi?post=18936;search_string=count%20no%20html;#18936]this thread) :


Quote
contents = contents.replace(/<(.+?)>/g, '');//Don't count HTML tags
contents = contents.replace(/&nbsp;/g, ' ');//Count nbsp; as one keystroke


For some reason I couldn't put all of the code into the plugin, I don't know how to call the text field from within the plugin?


demuire
Novice

Oct 13, 2004, 6:47 PM


Views: 69678
Re: [demuire] count char - max char

Actually, I should say that I don't know how to get the value of the HTMLArea box from within the plugin... I tried editor.getHTML() but it doesn't work? Might have to look at some other plugins and see how they work...


demuire
Novice

Oct 13, 2004, 8:28 PM


Views: 69676
Re: [demuire] count char - max char

Here's something I've come up with that seems to work...

In the calling page I have:


Quote
editor.registerPlugin(CharCounter, "counter");


where "counter" is the name of the text field that displays the number of characters

And this is what I have in char-counter.js:


Quote
function CharCounter(editor, params) {
this.editor = editor;
fieldID = params[0];
};

CharCounter._pluginInfo = {
name : "CharCounter",
version : "1.0",
developer : "Fook Lee",
developer_url : "http://www.fookstar.com",
c_owner : "Fook Lee",
sponsor : "",
sponsor_url : "",
license : "htmlArea"
};

CharCounter.prototype.onGenerate = function() {
var self = this;
var doc = this.editordoc = this.editor._iframe.contentWindow.document;
HTMLArea._addEvents(doc, ["keypress"],
function (event) {
return self.keyPress(editor, HTMLArea.is_ie ? self.editor._iframe.contentWindow.event : event);
});
};

CharCounter.prototype.keyPress = function(editor, ev) {
var self = this;
var i18n = CharCounter.I18N;

var contents = editor.getHTML();
contents = contents.replace(/<(.+?)>/g, '');//Don't count HTML tags
contents = contents.replace(/&nbsp;/g, ' ');//Count nbsp; as one keystroke
var counterField = document.getElementById(fieldID);
counterField.value = contents.length;
}

var fieldID = null;


It more or less works, there's something strange with the way it counts when you put carriage returns in, but otherwise it seems to work fine?

Hope this helps someone...


BRULIK
New User

Feb 20, 2005, 11:33 AM


Views: 69383
Re: [demuire] count char - max char

  1. button "Backspase" not counted :(
  2. HTML tags not counted :(


This plugin is verry necessary !!! Please change this code Wink

Thanks.


demuire
Novice

Feb 20, 2005, 6:47 PM


Views: 69362
Re: [BRULIK] count char - max char

What do you mean by backspace not counted?

Secondly if you want to count HTML tags just take out the code that says "don't count HTML code"!


BRULIK
New User

Feb 21, 2005, 1:52 AM


Views: 69349
Re: [demuire] count char - max char

If im use to 'Del' or 'Backspase' button , a count not autodecrement :(


demuire
Novice

Feb 21, 2005, 1:58 AM


Views: 69348
Re: [BRULIK] count char - max char

Really? Interesting, I'll have to look into that.


BRULIK
New User

Feb 21, 2005, 2:02 AM


Views: 69347
Re: [demuire] count char - max char

Beside you work?