Forget console.log

I remember the day I learned that I could use console.log to test my app instead of having to alert everything. Man did I feel good and smart. Everything was very smooth, and fast. No longer did I have to close every single alert in order to get to the next one, nor did I had to worry about leaving some rouge alert saying You made it into the if!

But then again every time I tried to test in IE beauty became the beast. Instead of getting a good log in the IE console which… ooops doesnt exist, I got a nasty js error saing what the hell is console.log ?

So I got into the habit of cleaning up my console.logs after using them which really didn’t help me testing out something in staging or dev because when you are making heavy client side apps, everything is ajax based, and you always need to know if you are showing the exact data that you received or if your js code is doing something funky.

Solution simply use another function that checks if console.log exists and if so log it, else don’t


function cLog( text ){
if( (window['console'] !== undefined) ){
console.log( text );
}
}

Updated with a better solution

if(!this.console){
window.console={
log:function(){}
};
}

Enjoy!

About these ads

8 thoughts on “Forget console.log

    • Great idea Alex!
      I guess you could put and absolute positioned element at the bottom of the screen to simulate as much as possible a typical console.log
      Then all you have to do is remove it from the DOM once you are finished. jQuery shouldn’t fire an error if the DOM element does not exist I believe.
      Or if we are already removing the div might as well remove the DOM console.log to I guess. But yeah this should work.

  1. I like the helpful information you provide in your articles.
    I’ll bookmark your weblog and check again here frequently.
    I’m quite sure I’ll learn a lot of new stuff right here!

    Good luck for the next!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s