Requirements
Forum Version : AnyOther : You must be using one of the following text editors :
- Default SCEditor
- Textarea with the ID text_editor_textarea
- None of the above ? You must modify it yourself, or request a modification !
Introduction
The purpose of this small plugin is to calculate the amount of characters you've typed + the characters that are remaining. If you exceed the character limit, you're most likely to receive a "Your post is too long" message. The goal is to keep you aware of your character count as you type.By default the following limits are set in place on every forum :
Signature : limit of 1,000 characters
Messages / PMs : limit of 60,000 characters ( this may vary across different forums )
Exceeding these limits may give you the aforementioned error message.
Installation
The installation is extremely simple ! Go to Administration Panel > Modules > JavaScript Codes managementNew ? If so, ensure that JS codes management is enabled by choosing "Yes." Otherwise, proceed to creating a new script with the following settings :
Title : Character counter
Placement : In all the pages
Paste the following script :
- Code:
$(function() {
if (!document.getElementById('text_editor_textarea')) return; // no textarea ? better not continue :(
// define global data to be cached and reused
window.$fa_char = {
area : document.getElementById('text_editor_textarea'), // message textarea
current : 0, // current characters typed
maximum : /page_profil=signature/.test(window.location.search) ? 1000 : 60000, // maximum characters allowed
used : null, // node cache for used characters
remain : null, // node cache for remaining characters
instance : null, // sceditor instance
// calculate the characters used and remaining
calculate : function() {
$fa_char.current = ($fa_char.instance ? $fa_char.instance.val() : $fa_char.area.value).length; // get the message length
$fa_char.used.innerHTML = $fa_char.current; // update the current count
$fa_char.remain.innerHTML = '<span ' + ($fa_char.current >= $fa_char.maximum ? 'style="color:#F00"' : '') + '>' + ($fa_char.maximum - $fa_char.current) + '</span>'; // update the remaining characters
}
};
var node = document.createElement('DIV'); // container for the chararacter data
node.id = 'faCharCounter'; // the id is mostly used for user defined styles
node.innerHTML = '<span id="faCharUsed">0</span> characters used out of ' + $fa_char.maximum + ' (<span id="faCharRemain" style="color:#090">' + $fa_char.maximum + '</span> remaining)'; // define our character data
$fa_char.area.parentNode.insertBefore(node, $fa_char.area); // insert the container before the textarea
// update the node caches so we don't have to keep getting these elements
$fa_char.used = document.getElementById('faCharUsed');
$fa_char.remain = document.getElementById('faCharRemain');
// execute another doc ready to match up with the sceditor
$(function() {
// depending if the sceditor is present, one of these events will be attached
if ($.sceditor) {
var container = $('.sceditor-container');
$fa_char.instance = $($fa_char.area).sceditor('instance');
$('textarea', container)[0].oninput = $fa_char.calculate; // source
$('iframe', container).contents()[0].body.oninput = $fa_char.calculate; // wysiwyg
} else $fa_char.area.oninput = $fa_char.calculate;
$fa_char.calculate(); // get the current character count on page load
});
});
Modifications
- Maybe the character limit is different for your forum and you need to change it ? All you need to do is replace 60000 in the script with your limit !
- Don't like the wording or need to translate it ? Find the following snippet and change it to your heart's content !
- Code:
node.innerHTML = '<span id="faCharUsed">0</span> characters used out of ' + $fa_char.maximum + ' (<span id="faCharRemain" style="color:#090">' + $fa_char.maximum + '</span> remaining)';
- Need a more stylish counter ? Never fear ! There's CSS selectors here !
- #faCharCounter : The main container for the character data
- #faCharUsed : The total amount of characters used
- #faCharRemain : The total amount of characters remaining
Even more selectors can be added simply by modifying the data in modification 2 !
Technical
The script creates a global object -- $fa_char -- to hold data, but also cache it for reuse so some conditions and traversal don't have to reoccur. It includes one method, calculate(), which can be used to update the character count. This method is executed every time the user lifts a key.The instance is mainly used to execute methods of the SCEditor. Otherwise, you should use area to access the value if you're not using the SCEditor. ( $fa_char.area.value )
Closing
If you have any questions or encounter any problems, please feel free to leave a reply. However, please ensure that you provide sufficient information so that I may answer your questions swiftly.Suggestions are always welcomed, of course.
Last edited by Ange Tuteur on June 28th 2015, 3:43 am; edited 2 times in total