How to inspect javascript Object property

This is a useful tool whenever you work with JavaScript and the compatibility issues between navigators.

You can inspect any JavaScript objects and list them as indented, ordered by levels.
It shows you type and property name. If an object property can’t be accessed, an error message will be shown.

Using the Code

function inspect(obj [, maxLevels [, level]])

Input Vars

obj: Object to inspect
maxLevels: Optional. Number of levels you will inspect inside the object. Default MaxLevels=1
level: RESERVED for internal use of the function

Return Value

HTML formatted string containing all values of inspected object obj.

function inspect(obj, maxLevels, level)
{
	var str = '', type, msg;	
	// Start Input Validations	
	// Don't touch, we start iterating at level zero	
	if(level == null) level = 0;	
	// At least you want to show the first level	
	if(maxLevels == null) maxLevels = 1;	
	if(maxLevels < 1)	
	return '<font color="red">Error: Levels number must be > 0</font>';	
	// We start with a non null object	
	if(obj == null)	
	return '<font color="red">Error: Object <b>NULL</b></font>';	
	// End Input Validations	
	// Each Iteration must be indented	
	str += '<ul>';	
	// Start iterations for all objects in obj

	for(property in obj)
	{
		try	
		{	
			// Show "property" and "type property"
			
			type = typeof(obj[property]);	
			str += '<li>(' + type + ') ' + property +	
			( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';	
			// We keep iterating if this property is an Object, non null	
			// and we are inside the required number of levels	
			if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))	
			str += inspect(obj[property], maxLevels, level+1);	
		}
	
		catch(err)	
		{	
			// Is there some properties in obj we can't access? Print it red.		
			if(typeof(err) == 'string') msg = err;		
			else if(err.message) msg = err.message;		
			else if(err.description) msg = err.description;		
			else msg = 'Unknown';		
			str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';	
		}
	}

	// Close indent
	str += '</ul>';
	return str;
}
 

Date difference interms of year/month/week/day/hour/minute/seconds

Some times we need to calculate date difference between 2 dates  in the format such as how many years or months or weeks or days or hours or minutes or seconds left from a particular date. Here is nice and simple solutions for this type of problem.


function compare_dates($date1, $date2) {
     $blocks = array(
         array('name' => 'year', 'amount' => 60 * 60 * 24 * 365),
         array('name' => 'month', 'amount' => 60 * 60 * 24 * 31),
         array('name' => 'week', 'amount' => 60 * 60 * 24 * 7),
         array('name' => 'day', 'amount' => 60 * 60 * 24),
         array('name' => 'hour', 'amount' => 60 * 60),
         array('name' => 'minute', 'amount' => 60),
         array('name' => 'second', 'amount' => 1)
     );

$diff = abs($date1 - $date2);

$levels = 2;
     $current_level = 1;
     $result = array();
     foreach ($blocks as $block) {
         if ($current_level > $levels) {
             break;
         }
         if ($diff / $block['amount'] >= 1) {
             $amount = floor($diff / $block['amount']);
             if ($amount > 1) {
                 $plural = 's';
             } else {
                 $plural = '';
             }
             $result[] = $amount . ' ' . $block['name'] . $plural;
             $diff -= $amount * $block['amount'];
             $current_level++;
         }
     }
     return implode(' ', $result);
 }

[NB: $date1 & $date2 must be in time stamp]

Improve your Jquery Performance

Here is 25 excellent Tips that will improve your jquery performance.

1.  Load the framework from Google Code
2.  Use a cheat sheet
3.  Combine all your scripts and minify them
4.  Use Firebug’s excellent console logging facilities
5.  Keep selection operations to a minimum by caching
6.  Keep DOM manipulation to a minimum
7.  Wrap everything in a single element when doing any kind of DOM insertion
8.  Use IDs instead of classes wherever possible
9.  Give your selectors a context
10.  Use chaining properly
11.  Learn to use animate properly
12.  Learn about event delegation
13.  Use classes to store state
14.  Even better, use jQuery’s internal data() method to store state
15.  Write your own selectors
16.  Streamline your HTML and modify it once the page has loaded
17.  Lazy load content for speed and SEO benefits
18.  Use jQuery’s utility functions
19.  Use noconflict to rename the jquery object when using other frameworks
20.  How to tell when images have loaded
21.  Always use the latest version
22.  How to check if an element exists
23.  Add a JS class to your HTML attribute
24.  Return ‘false’ to prevent default behaviour
25.  Shorthand for the ready event

This article was orginally posted on http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx.

CodeIgnite htaccess…

Those who want to work on codeigniter may have face some problems when you want change the default url pattern. It’s a problem, But don’t worry  only a couple of lines code will solve that problem.

Just create a .htaccess file in your codeigniter root directory and then simply paste the code.

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]