How to check if an object is empty?

You have a variable of type object in JavaScript. You want to check if it is empty. If the variable is null you can check it like

if(variableName == null )
{
    //do something.....
}

but this will not check if the object is empty.

Use this function to check if the object is empty.

function isObjectEmpty(object)
{
  var isEmpty = true;
  for(keys in object)
  {
     isEmpty = false;
     break; // exiting since we found that the object is not empty
  }
  return isEmpty;
}

How to use the function


var myObject = {}; // Object is empty
var isEmpty  = isObjectEmpty(myObject); // will return true;

// populating the object
myObject = {"name":"John Smith","Address":"Kochi, Kerala"}; 

// check if the object is empty
isEmpty  = isObjectEmpty(myObject); // will return false;
Posted in JavaScript, Web development | Tagged , , , | Leave a comment

Truncate string and append dots using Javascript

How to truncate a string and append 3 dots in JavaScript.

function add3Dots(string, limit)
{
  var dots = "...";
  if(string.length > limit)
  {
    // you can also use substr instead of substring
    string = string.substring(0,limit) + dots;
  }

    return string;
}

Call like this

add3Dots("Hello World",9);

The above function call returns this string

Hello Wor...

 

A minified version of the above function is given here

function add3Dots(string, limit)
{
   return string = string.length > limit ? string.substring(0,limit) + "...": string;
}

A über minified version (with the price of readability) is given here


function aD(s,l){return s=s.length>l?s.substring(0,l)+"...":s;}

In this case you need to call like this…


aD("Hello World",9);
Posted in JavaScript | Tagged , , | Leave a comment

Object copying in jQuery

Shallow copy

var newObject = jQuery.extend({}, oldObject);

Deep copy

var newObject = jQuery.extend(true, {}, oldObject);

What is shallow copy and deep copy?

If you need to copy(clone) object a  to object b  in Javascript using jQuery, do like this.

// old object
var a = {"name":"John Smith", "age":"30"};

// new object, we need to copy the contents of object a to object b
var b =  jQuery.extend(true, {}, a);

This is a sample program

// old object
var a = {"name":"John Smith", "age":"30"};

console.log("Printing the contents of object a in Firebug console");
console.log(a);

// new object, we need to copy the contents of object a to object b
var b =  jQuery.extend(true, {}, a);

console.log("Printing the contents of object a in Firebug console");
console.log(b);

// Now we are editing the contents of object a
a.name = "Mary Smith";
a.age = "31"

// Print the object again
console.log("Printing the contents of object a in Firebug console");
console.log(a);
console.log("Printing the contents of object b in Firebug console");
console.log(b);

// Note that even though we changed the contents of object a, the contents of object b is not changed.

The output in firebug console will look like this.

Printing the contents of object a in Firebug console
Object { name="John Smith", age="30"}
Printing the contents of object a in Firebug console
Object { name="John Smith", age="30"}
Printing the contents of object a in Firebug console
Object { name="Mary Smith", age="31"}
Printing the contents of object b in Firebug console
Object { name="John Smith", age="30"}
Note that even though we changed the contents of object a, the contents of object b is not changed.

Hope this information is useful to you.

Posted in JavaScript, jQuery, Web development | Tagged , | Leave a comment

AS3 – Access root from a movie clip

Use like this to access root from a movie clip

MovieClip(root).someThing;

For example suppose you have a text box in root with instance name txtAddress in your root. If you need to set the text of txtAddress from a movie clip the code should be like

MovieClip(root).txtAddress.text = "Some text here."
Posted in ActionScript | Tagged , , | 4 Comments

ActionScript : Find code execution time

Using the script below you can find how much time it took to execute your ActionScript code

var startTime:Date = new Date();

// execute your code here

var endTime:Date = new Date();
trace("Time take to execute the code "+String(endTime.getTime()-startTime.getTime())+" milliseconds");

Check PHP version here

Check Javascript version here

Posted in ActionScript, Flash/Flex, Web development | Tagged , , | Leave a comment