2010/11/12

Woah!

So I bit the bullet and installed MSIE 8 on my Windows VM. A task that would have been made easier if the download page didn't crash MSIE 6. Brilliant move there.

Today I was poking around a bit and discovered some startling things: A JavaScript debugger, logging console, CSS and DOM viewer and more! WHO'S A HAPPY PROGRAMMER NOW?

Mind you the MSIE Dev Tools are equivalent to a 3 year old version of Firebug. And the dev tools are pretty slow, about as slow as a ... a very slow thing. I'm going to have to up the resources to that VM.

And of course, this being MS, they go and muck up the console.log API.

The following works as expected:
console.log("Something");
This next snippet however fails:
console.log.apply(console,arguments);
Someone please track down the programmer who wrote this and all the team leads and suits who signed off on it and SLAP THEM UPSIDE THE HEAD! .apply has been part of JavaScript since 1.3, which was released back in the previous millennium. So we get yet another arbitrary work arounds for MSIE.

Because I can't just go sprinkle console.log() through out the code; many/most users will not have Firebug or MSIE 8 installed.
function fb_log () {

if( window['console'] && window['console']['log'] ) {
if( window['console']['log']['apply'] ) {
console.log.apply( console, arguments );
}
else {
// Assume this is the MSIE 8 console
console.log( fb_format( arguments ) );
}
}
}

Firebug has printf-like formatting of console.log output. For MSIE 8 we have to do it by hand:
function fb_format ( args ) {

var N=1;
var string = args[0];
if(typeof string == "object" ) {
return fb_object( string );
}

return string.replace( /%([sdi])/g,
function (str, p1, offset, s) {
var ret = '';
if( p1 == 's' ) {
ret = args[N];
}
else {
ret = args[N].toString();
}
N++;
return ret;
} );
}

Firebug also has some magic for logging objects:
function fb_object (obj) { 

var a = [];
for( var k in obj ) {
if( typeof obj[k] == 'string' ) {
a.push( k+': "'+obj[k].replace(/"/g, '\\"' )+'"' ); // "
}
else {
a.push( k+': '+obj[k].toString() );
}
}
return "{ "+a.join( ', ' )+" }";
}

No comments: