I hope the following could help you while it's not functional complete:
var editor = CKEDITOR.instances.story2;
editor.on( 'key', function( evt ){
// Update the counter with text length of editor HTML output.
textCounter2( { value : evt.editor.getData() },this.form.grLenght2, 200 );
}, editor.element.$ );
Poirot wrote:Thanks Gary, that works like you can see at http://romza.com/counter.html. It seems to be a little bit erratic, as in maybe a few characters out sometimes, but it will do for what I need it for.
To Gytha_Ogg: have a look at the code/source of [url]Gytha_Ogg[/url]. Hopefully that gives you some idea where to put the code.
This code works properly, anyway, I would prefer to prevent people from typing more characters than the number allowed. Is it possible ?
While it's not an easy-to-achieve functionality, a solution could be found based on the editor's undo/redo system, I hope the following codes could be a rough idea for you while it's not functional complete( require modification when working with IME):
// Whether content has exceeded the maximum characters.
var locked;
editor.on( 'key', function( evt ){
var currentLength = editor.getData().length,
maximumLength = 200;
if( currentLength >= maximumLength )
{
if ( !locked )
{
// Record the last legal content.
editor.fire( 'saveSnapshot' ), locked = 1;
// Cancel the keystroke.
evt.cancel();
}
else
// Check after this key has effected.
setTimeout( function()
{
// Rollback the illegal one.
if( editor.getData().length > maximumLength )
editor.execCommand( 'undo' );
else
locked = 0;
}, 0 );
}
} );
garry.yao wrote:While it's not an easy-to-achieve functionality, a solution could be found based on the editor's undo/redo system, I hope the following codes could be a rough idea for you while it's not functional complete( require modification when working with IME):
There is only a little problem with Opera : when you reach the limit, the last letters of the text are replaced by what you are trying to type. I like Opera but I have to admit that it has always been quite touchy about javascript.
I'm using the following expression to count the characters, since the max length isn't high for me (around 200 characters max.). I didn't made any performance test for the moment.
I follow those steps : 1. remove html tags 2. replace multiple spaces by only one 3. consider HTML entities (é for example) as one character (artificially replaced by the character 'X') 4. remove white spaces at the beginning and end of the text.
Then I begin listening on events. Here is when I get into trouble. I'm currently listening on the event 'key' but it seems that the data returned by 'editor.getData()' is the data before modification.
Is there an event corresponding to 'after content modification' ?
Re: Character count
ticket #397
Re: Character count
Re: Character count
var editor = CKEDITOR.instances.story2; editor.on( 'key', function( evt ){ // Update the counter with text length of editor HTML output. textCounter2( { value : evt.editor.getData() },this.form.grLenght2, 200 ); }, editor.element.$ );Re: Character count
http://sourceforge.net/tracker/index.php?func=detail&aid=1510662&group_id=75348&atid=737639
Re: Character count
http://demo.goldroo.com/postnews/
Re: Character count
to prevent
Re: Character count
While it's not an easy-to-achieve functionality, a solution could be found based on the editor's undo/redo system, I hope the following codes could be a rough idea for you while it's not functional complete( require modification when working with IME):
// Whether content has exceeded the maximum characters. var locked; editor.on( 'key', function( evt ){ var currentLength = editor.getData().length, maximumLength = 200; if( currentLength >= maximumLength ) { if ( !locked ) { // Record the last legal content. editor.fire( 'saveSnapshot' ), locked = 1; // Cancel the keystroke. evt.cancel(); } else // Check after this key has effected. setTimeout( function() { // Rollback the illegal one. if( editor.getData().length > maximumLength ) editor.execCommand( 'undo' ); else locked = 0; }, 0 ); } } );Re: Character count
I thank you, Garry. I'm going to test this.
Re: Character count
Re: Character count
There is only a little problem with Opera : when you reach the limit, the last letters of the text are replaced by what you are trying to type. I like Opera but I have to admit that it has always been quite touchy about javascript.
Re: Character count
Re: Character count
http://www.lgv.on-web.fr/valinfotest/testcke.php
Re: Character count
I didn't made any performance test for the moment.
I follow those steps :
1. remove html tags
2. replace multiple spaces by only one
3. consider HTML entities (é for example) as one character (artificially replaced by the character 'X')
4. remove white spaces at the beginning and end of the text.
Then I begin listening on events. Here is when I get into trouble. I'm currently listening on the event 'key' but it seems that the data returned by 'editor.getData()' is the data before modification.
Is there an event corresponding to 'after content modification' ?
Re: Character count
editor.on('key, function(evt) { var currentLength = editor.getData().length, maximumLength = 200; if (currentLength >= maximumLength) { if (!locked) { // Record the last legal content. editor.fire('saveSnapshot'); locked = 1; // Cancel the keystroke. evt.cancel(); } else // Check after this key has effected. setTimeout(function() { // Rollback the illegal one. if (editor.getData().length > maximumLength) { editor.execCommand('undo'); if (editor.getData().length > maximumLength) { editor.setData(editor.getData().substring(0, maximumLength )); } } else { locked = 0; } }, 0); }Re: Character count
The Counter doesn't work on paste a text from (e.g) MS Word. If there any option to add a event handler to do this, please let me know.
Thank you!
problem on paste
I have same problem.
I used this solution but problem is that still not working on paste frome another source.
Is there any good solution for this?
I can see that this topic is old so it should be.
Thank you.