Archive for January, 2010

Getting your friend names: use users_getinfo() not FBML

Wednesday, January 27th, 2010

To get the names of the user’s friend, you can go two ways:
1/ make a Facebook API call on the server side using users_getinfo()
2/ let Facebook render the names in FBML using the fb:name tag and passing it the user id

While it’s tempting to speed up your application by removing the users_getinfo() call and going for solution 2/, in practice 2/ will turn out slower.

You won’t notice the difference when the users doesn’t have a lot of friends, and in this case 2/ may be faster.

But past 200 friends, and 2/ is extremely slow.

An extra goodie with 1/ is that you also get access to the friends names on the server side, so you can do more processing.

Facebook Ajax call hitting your URL 3 times in a row

Thursday, January 21st, 2010

Did you notice Ajax calls hitting your url three times in a row?

This will happen if the URL ajax.post() is hitting doesn’t return anything at all.

var ajax = new Ajax();
ajax.responseType = Ajax.RAW;
ajax.requireLogin = false;
ajax.ondone = null;
ajax.onerror = function() {}

var params={
    'title':'Nice title',
    'comment': 'Nice comment'
};
ajax.post('http://example.com/my_ajax_handler.php',params);

If your my_ajax_handler.php script doesn’t return a single string, ajax.post will hit you 3 times.

Just return something like ‘1′ for instance at the end of your my_ajax_handler.php script and you will be hit only once:

<?php
 if(count($_POST)) {
     $title = $_POST['title'];
     // process your data...
 }
?>
1

Notice the ‘1′ outside of the PHP block at the end.

IE bug: text disappearing randomly behind a picture

Wednesday, January 20th, 2010

Even though the text had a high z-index, it would disappear after few seconds behind the picture it’s supposed to be on, or when you switch to another Windows application then come back on the browser.

This erratic behavior turned out to be when IE doesn’t have a Doctype declared.

CakePHP doesn’t define any Doctype by default, so you need to edit your default layout and include it.

In CakePHP 1.1: edit app/views/layout/default.hthml

In CakePHP 1.1: edit app/views/layout/default.ctp

and add:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

… at the very top, before the html tag.

This happens not even on an old IE6, but on IE8 itself.

Javascript var_dump() for debug

Tuesday, January 19th, 2010
/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

Source

IE bug: don’t put extra commas in your JS arrays

Monday, January 18th, 2010

Here’s a simple Javascript array:

var params={
         'uid':uid,
         'comment':'hello',
};

Nothing fancy except that after the ‘hello’ string, there’s an extra comma that should not be there.

Firefox and Chrome don’t care, it still works.

But IE7 hates it, crashes the page and throws and error, with a ludicrous line number in it (did I write that much code?):

Message: Expected identifier, string or number  Line: 225068230

Remove the extra comma and you’re done.

Note that you can see the problem in IE8 also by enabling debugging (hit F12) and set the Document Mode to IE7 Standards.