WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Word Count JavaScript

[Resolved] Word Count JavaScript

  • Hello,

    I found the following JavaScript in my WordPress account for counting words as you type on the write screen, but I’ve no idea how to integrate it to my own copy of WordPress on my domain.

    The file is apparently named “wordcount.js-102007” or something close to that.

    Any idea on how to use this???

    jQuery( function($) {
    
    	var w = $("<span class='wp-word-count'>" + wordCountL10n.count.replace( /%d/, "<span id='word-count'></span>" ) + "</span>").prependTo('#poststuff p.submit:first');
    
    	var block = 0;
    	var wc = function(text) {
    		if ( block ) return;
    		block = 1;
    
    		setTimeout( function() {
    			var t = $.trim(text);
    			var tc = '0';
    			if ( t ) {
    				t = ' ' + t.replace( /<.[^<>]*?>/g, '' ) + ' ';
    				tc = t.split( /\s[\s.(),;:!?'"_+=\\/-]*/ ).length - 2;
    				tc = tc.toString();
    			}
    
    			w.children('span')[0].innerHTML = tc;
    
    			setTimeout( function() { block = 0; }, 2000 );
    		}, 1 );
    	};
    
    	var fwc = function() {
    		var t =  parseInt(w.children('span').text()) + 1;
    		w.children('span').text( t.toString() );
    	};
    
    	var last = 0;
    
    	var co = $('#content').keyup( function(e) {
    		if ( e.keyCode == last )
    			return true;
    		if ( 13 == e.keyCode )
    			wc( co.val() );
    		else if ( 32 == last )
    			fwc();
    		last = e.keyCode;
    		return true;
    	} );
    
    	wc(co.val());
    
    	if ( 'undefined' == typeof TinyMCE_wordpressPlugin ) { return; }
    	var o = $.isFunction(TinyMCE_wordpressPlugin.handleEvent) ? TinyMCE_wordpressPlugin.handleEvent : function() { return true; };
    
    	TinyMCE_wordpressPlugin.handleEvent = function(e) {
    		if ( !o(e) )
    			return false;
    		if ( 'keyup' == e.type ) {
    			if ( e.keyCode == last )
    				return true;
    			if ( 13 == e.keyCode )
    				wc( tinyMCE.getInstanceById('content').getHTML() );
    			else if ( 32 == last )
    				fwc();
    			last = e.keyCode;
    		}
    		return true;
    	};
    
    } );
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Word Count JavaScript’ is closed to new replies.
Skip to toolbar