Archive for March, 2010

Converting String to Single in C#

Some time ago, I needed to get Flash Movie sizes inside of my C# code. I googled and I found some great examples of how I can do it. It was Mike Swanson’s example project called FlashTools. I downloaded it and stated to use. While I was using this code I found a little bug that was occured depending of regional settings in the system. An error occured when FlashTools library tryed to convert String to Single. When I used English locale everything was going OK but when I changed the locale I got “Input string was not in a correct format”. A fix was very simple, I just passed the InvariantCulture as the second parameter of Convert.ToSingle method.
Read the rest of this entry »

Compatibility Mode doesn’t work in Internet Explorer 8.. Why?

Today I had a problem with IE8. Compatibility Mode for IE7 didn’t work when I was working with the Artisteer generated theme that I was improving by hands. The tag order does matter but I have never thought it would.

Read the rest of this entry »

How to test for an empty javascript object

Hi everybody! I’ve just had a discussion of how to test for an emtpy javascript object. As a result, I have several code snippets and I want to share them.

Problem

1
2
3
4
5
6
7
  alert({} == null); // false
  alert({} == {}); // false
  alert({} == {}); // false
  alert({} == 0); // false
  alert({} == ""); // false
  alert({} == []); // false
  // etc :)

So, none of the expressions above can check for an empty javascript object.

Pure javascript solution

1
2
3
4
5
6
7
  function empty(o) {
    for(var i in o) 
      if(o.hasOwnProperty(i))
        return false;
 
      return true;
  }
object.hasOwnProperty(property) Returns a boolean indicating whether the object has the specified property. This method does not check down the object’s prototype chain.

Learn details of hasOwnProperty here at developer.mozilla.org.

JQuery + JQuery.JSON solution

1
2
3
4
5
  var o = {};
  alert($.toJSON(o) == '{}'); // true
 
  var o = {a: 1};
  alert($.toJSON(o) == '{}'); // false

The code above simply converts an object to JSON string.

UPDATE: jQuery.isEmptyObject method

As of jQuery 1.4, there’s isEmptyObject method to detect is the object empty. Look at the followtin code for example:

1
2
  $.isEmptyObject({}); // true
  $.isEmptyObject({someprop: "true"}); // false

Your way

Well, if you have your way of checking for an empty javascript object I am waiting your comments! Thanks in advance!

Links

jQuery.isEmptyObject

hasOwnProperty documentation

JQuery JavaScript Library