jQuery debug plugin

Console.log()

Almost everybody using Firebug extension of Firefox is familiar with console object and in common with console.log method, who’s taking an object parameter and dumps it into the Firebug console. It’s perfect for debugging and it’s really useful.The problem is that if you don’t have Firebug installed or it’s disabled the console object is undefined. The same may occur if you’re using other browser. That’s why I decided to make a simple plugin for jQuery with the $.log interface which checks for the console and opera objects and if they don’t exists just alert the message. It’s nothing special.

jQuery log

the code is really simple and small:

(function(jQuery) {

	/**
	 * log
	 *
	 * write debug errors to the console or
	 * alert them if the browser does not support
	 * the console object
	 *
	 * @public
	 * @param {Object}
	 * @return {Void}
	 */
	var log = function( object ) {
		if ( typeof console == 'object' )
			console.log( object );
		else if ( typeof opera == 'object' )
			opera.postError( object );
		else
			alert(object);
	}

	jQuery.fn.log = log;
	jQuery.log = log;

})(jQuery);

Demo page

The demo of the page is here.

Related posts:

  1. Writing a jQuery plugin – (part 2). Sample plugin.
  2. jQuery browser and OS detection plugin
  3. How to detect a variable existence in JavaScript?
This entry was posted in javascript, micro tutorial, portfolio, web development. Bookmark the permalink.

One Response to jQuery debug plugin

  1. unloco says:

    Great Job :) nice code
    very useful

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">