WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Word Count JavaScript (4 posts)

  1. michaelpark
    Member
    Posted 6 years ago #

    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;
    	};
    
    } );
  2. michaelpark
    Member
    Posted 6 years ago #

    Anyone?

  3. michaelpark
    Member
    Posted 6 years ago #

  4. Storyman
    Member
    Posted 6 years ago #

    Michael,

    Did you ever get it working?

Topic Closed

This topic has been closed to new replies.

About this Topic