Feb/080
Quick Tip, “Firebug breakpoints not breaking!”
When using FireBug to step through your JavaScript code in FireFox, sometimes it does not break on a line that you know is being executed. If you are like me you are like WTF, what is going on? I eventually realized that the reason FireBug was not breaking on some lines of my code was that the code I set my breakpoint on was inline and not inside a function. Apparently FireBug will not break on a line that is not inside a function. A quick fix for this is to wrap your code in a function and call the function immediately after declaring the function.
Instead of:
1
2 var foo = "stuff";
alert(foo);
Use:
1
2
3
4
5 function showFoo(){
var foo = "stuff";
alert(foo);
}
showFoo();
In the second block you can put breakpoints on either line inside the function showFoo() and FireBug will break when you expect it to.
For more on debugging JavaScript in Firefox check my Firebug post or my article on conditional break points.
Feb/080
The MooTools Ajax class, the easiest path to dynamic content.
The guys over at MooTools created a whole series of remote server JavaScript classes that make it easier to communicate asynchronously with the server. One of the easiest is the object found in MooTool’s Ajax.js file aptly named the Ajax object.
The Ajax object is not really designed for complex handling of the response from the server; it is really a down and dirty go here and put what you find there in this element kind of object. So the basic syntax is:
1
2
3
4
5 <div id="showTime"></div>
<script language="JavaScript">&gr;
var myAjax = new Ajax('http://www.sweetvision.com/servertime.php', {update: $('showTime')});
myAjax.request();
</script>&gr;
In the preceding code we are creating a new Ajax object that accesses the URL ‘http://www.sweetvision.com/servertime.php’ and what ever is retuned by that URL is injected into the DIV identified by “showTime”. Request() is the member function of the Ajax object that actually sends the request, the object handles the rest.
Another thing that the Ajax object simplifies for you is sending the contents of a form asynchronously. If you have included Ajax in your download of MooTools then any time you reference a form with $(‘formID’) it automatically has an Ajax object in it and you can use $(‘formID’).send() to send the form to the server. Send() takes the same options that the Ajax() constructor takes so you can use update: just like in the example above to display the results of your form submission on your page. You also might want to pass in a function to the onComplete event and hide the form when the ajax request is completed.