Translations of this page?:

Adding a Wordcounter to the Editform

Adding the following JavaScript code to your conf/userscript.js will add an automatic word counter at the bottom of the edit textarea. The count is updated on each keyup event and will show the word count of the currently edited section and the overall word count of the whole page.

/**
 * Script to add a wordcounter on the edit form
 *
 * @author  Andreas Gohr <andi@splitbrain.org>
 * @license GPL 2
 */
 
function wordcounter(text){
    var list = text.split(/[^\w\-_]+/|>);
    var len  = list.length;
    if(list[len-1] == '') len--;
    if(list[0] == '') len--;
    if(len < 0) len=0;
    return len;
}
 
addInitEvent(function(){
    var form = $('dw__editform');
    if(!form) return;
 
    var div = document.createElement('div');
    div.id = 'word__counter__output';
    div.style.position = 'absolute';
    div.style.width    = '200px';
    div.style.left     = (findPosX(form.elements.wikitext)+2)+'px';
    div.style.top      = (findPosY(form.elements.wikitext)+
                          form.elements.wikitext.offsetHeight-2)+'px';
    div.style.color    = '#0d0';
 
    form.appendChild(div);
    var all = wordcounter(form.elements.prefix.value);
        all += wordcounter(form.elements.suffix.value);
 
    addEvent(form.elements.wikitext,'keyup',function(){
        var len = wordcounter(form.elements.wikitext.value);
        div.innerHTML = len+'/'+(all+len)+' words';
    });
});
 
wiki/tips/wordcounter.txt · Last modified: 2006/12/15 21:49 by andi
 
Imprint Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsTranslate