Archive for the ‘Javascript’ Category

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.

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.

Disable Javascript caching in Firefox

Wednesday, December 24th, 2008

In about:config set:

network.http.use-cache = false

Source

MySpace app auth problem

Thursday, November 6th, 2008

Here’s the message I got:

Fatal error: Uncaught exception 'Exception' with message '401 <error xmlns="api-v1.myspace.com"><statuscode>401</statuscode><statusdescription>User has not installed the current application or application does not have access to requested resource</statusdescription></error>' in /BaseAPI.php:62 Stack trace: #0

The problem was that MS_API_KEY changed, so just update it with the new “OAuth Consumer Key:” on the myspace dev app page and you’re done.

Javascript date picker

Wednesday, November 5th, 2008

Here’s a Javascript date picker that works.

Randomize an array in Javascript

Monday, August 4th, 2008
function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

Source