1. Using Prototype??™s $$() function, it??™s easy enough. Let??™s toss in a few trace() calls as
well; it will give you a running tally of what??™s going on in the script. For more on using
trace(), see the sidebar ???Using jsTrace.???
initialize: function(){
trace( 'initialize()' );
// Collect the DLs & loop
$$( 'dl.faq' ).each( function( dl ){
trace( 'DL loop' );
// magic goes here
}.bind( this ) ); // End DL loop
},
nNote If you feel $$() is too slow for this purpose, you can always use old-school document object model
(DOM) methods to do the same thing. Just be sure to make your results enumerable and then skip any dl
that does not have a class of faq.
CHAPTER 8 n CASE STUDY: FAQ FACELIFT 175
USING JSTRACE
Inspired by the trace() method in JavaScript??™s cousin, ActionScript, jsTrace is a web page overlay that provides
a quick rundown on what??™s occurring in your scripts as it happens. To set it up, you simply define the
trace() function and set it to send a message to the jsTrace window if jsTrace exists.
var trace;
if( typeof( jsTrace ) != 'undefined' ){
trace = function( msg ){
jsTrace.send( msg );
};
} else {
trace = function(){ };
}
By defining trace() as a null function when jsTrace is undefined, you make it safe to remove or comment
out the jsTrace files without throwing JavaScript errors from trace() calls in the scripts. Of course,
any trace() calls should be removed as part of your script-optimization regimen, but during the development
process, it is very helpful to be able to turn jsTrace off and then on again easily.
Pages:
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249