Участник:Alex Smotrov/misc/charcounter.js
Перейти к навигации
Перейти к поиску
//On editpage displays textarea char/bytes counter
if (document.URL.match(/action=(edit|submit)/))
$(function(){
txtarea = document.getElementById('wpTextbox1'); //global var
if (!txtarea) return;
txtCounterSpan = document.createElement('span'); //global var
txtCounterSpan.id = 'txtCounterSpan';
txtCounterSpan.style.position = 'absolute';
txtCounterSpan.style.top = '100px';
txtCounterSpan.style.left = '-100px';
txtCounterSpan.style.zIndex = '100';
txtarea.form.style.position = 'relative';
txtarea.form.appendChild(txtCounterSpan);
addEvent(txtarea, 'keyup', countChars);
addEvent(txtarea, 'mouseup', countChars);
addEvent(txtCounterSpan, 'click', countBytes);
countChars();
countBytes();
})
function countChars(){
txtCounterSpan.innerHTML = txtarea.value.length;
}
function countBytes(){
var st = txtarea.value;
var bb, extra=0;
for (var i=0; i<st.length; i++)
if ((bb = st.charCodeAt(i)) > 127) {
extra++;
if (bb > 2048) extra++;
}
txtCounterSpan.innerHTML = st.length + ':' + (st.length + extra);
// + ':' + st.split(' ').length //number of words
}
function addEvent(obj, event, func) {
if (obj.addEventListener) obj.addEventListener( event, func, false );
else if (obj.attachEvent) obj.attachEvent ('on'+event, func)
}