Code of the Day: Javascript Auto-complete date format MMDDYYYY

/**
* This function helps to autocomplete the date format MMDDYYY
* Converts M to 0M and MMD to MM0D. Ex. `1/` to `01/`, `01/1/` to `01/01/`
* Adds slash for MM and MMDD Ex. `01` to `01/`, `01/02` to `01/02/`
* Converts YY to YYYY. Ex. `01/01/01` to `01/01/2001`
*
* @param {String} str
* @return {String}
*/
var autocompleteMMDDYYYYDateFormat = function (str) {
        str = str.trim();
        var matches, year,
                looksLike_MM_slash_DD = /^(\d\d\/)?\d\d$/,
                looksLike_MM_slash_D_slash = /^(\d\d\/)?(\d\/)$/,
                looksLike_MM_slash_DD_slash_DD = /^(\d\d\/\d\d\/)(\d\d)$/;
 
        if( looksLike_MM_slash_DD.test(str) ){
                str += "/";
        }else if( looksLike_MM_slash_D_slash.test(str) ){
                str = str.replace( looksLike_MM_slash_D_slash, "$10$2");
        }else if( looksLike_MM_slash_DD_slash_DD.test(str) ){
                matches = str.match( looksLike_MM_slash_DD_slash_DD );
                year = Number( matches[2] ) < 20 ? "20" : "19";
                str = String( matches[1] ) + year + String(matches[2]);
        }
        return str;
};

Demo

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Code of the day: jQuery, Get comments from HTML elements.

Code of the day: Get comments from HTML elements.
Here’s a simple jquery plugin to get the comments from a HTML elements.

// source code for $.fn.getComments()
$(function () {
/**
* $.fn.getComments() is used to extract the html comments from a HTML elements.
* 
* @author Larry Battle <http://bateru.com/news/contact-me>
* @license MIT
* @date June 11, 2012
* @version 0.1
* @args {boolean} asArray - If true, returns an array of the comments values. 
		Otherwise returns jquery objects of the node comments.
* @example 
	HTML:
	<div id="example">I am a div. <!--Duh!--></div>
 
	Javascript: 
	$("#example").getComments(true) // returns [ "Duh!" ]
*/
	var getCommentsFromEl = function (el, asArray) {
		var result,
		$el = $(el).contents();
		result = $el.filter(function () {
				return this.nodeType == 8;
			});
		if (asArray) {
			result = $.makeArray(result.map(function () {
						return this.nodeValue;
					}));
		}
		return result;
	};
	$.fn.getComments = function (asArray) {
		return getCommentsFromEl(this, asArray);
	};
});

Demo and testcases here: http://jsfiddle.net/96rux/

Fork this on Github.com
https://github.com/LarryBattle/jQuery.getComments

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Code of the day: Check to see if a element has a event.

// hasEvent checks to see if a element contains a event.
// @requires jQuery 1.3.2+
// @params el: string, jQuery object, node element.
// @params eventName: string, name of the event.
// @returns boolean
var hasEvent = function( el, eventName ){
	if( !$( el ).length || !$( el ).data( 'events' ) ){
		return false;
	}
	return !!$( el ).data( 'events' )[ eventName ];
};

Demo: (requires firebug or google chrome)

var el = $( '<div/>' ).click( $.noop );
console.clear();
console.log( hasEvent( el, 'click' ) );// returns true;
 
console.log( hasEvent( el, 'focusNow' ) );// returns false;
el.bind( 'focusNow', $.noop );
console.log( hasEvent( el, 'focusNow' ) );// returns true;
 
console.log( hasEvent( document.body, 'onload' ) );// returns false;
$(document.body).bind( 'onload', $.noop );
console.log( hasEvent( document.body, 'onload' ) );// returns true;

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Chickenfoot mixed with jQuery.

Chickenfoot is a Firefox extension design for website automation.

Chickenfoot is a Firefox extension that puts a programming environment in the browser’s sidebar so you can write scripts to manipulate web pages and automate web browsing. In Chickenfoot, scripts are written in a superset of Javascript that includes special functions specific to web tasks.

Basically, it let’s you take control of Firefox and simulate a user. The API is fill with basic operation, like go(url), append(content), onClick( function, window) and etc. You can get more power and functionality by including other JavaScript libraries and frameworks. That’s when the fun begins.

Since the goal in website automation is a lot of HTML manipulations then, jQuery is the best framework for the job. jQuery can be easily included with a simple fix here.
Here are some examples of Chickenfoot scripts. Once Chickenfoot is installed hit f8, press the “New” tab, then insert one of the following scripts.

Downloading a URL

include( "fileio.js" ); //Enables the write and read commands.
var url = "http://google.com",
content = read( url ),
filename = desktopDir().path + "\\google.html";

write( filename, content );

Clean up craigslist.org

include( "http://bateru.com/js/jquery-1.4.2-mod.js" );
$ = window.$;
var url = "http://houston.craigslist.org/sss/";
go( url, true ); //Refresh the page to the url
sleep(2); // So you can see the different.
var cleanHTML = $(document.body).html().toLowerCase();
cleanHTML = cleanHTML.replace( /[$\*!]{2}|[~\*]/gi, "");
$( document.body ).html( cleanHTML );

Overall, Chickenfoot is a great extension to have. These few examples shown here only scratch the surface of what it’s capable of.

Chickenfoot 1.0.7 link
jQuery link

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube