JavaScript logging in console and window

· 1 Comment · in JavaScript

I recently had the need to add logging to a real-time application using JavaScript.

Most developers usually throw in console.log or alerts during development. But if the consoles/alerts are not removed before code goes to production or a client, it can cause a big problem.

The following code example using console.warn provides this safely. If window.console or console.warn are not supported, no message is generated. But it does not cause the page to choke, like using a simple console.log.

I’ve chosen console.warn here, but you can use console.info, etc. I suggest you test it thoroughly before deploying to any live application.

window.console && console.warn('Warning: someVariable does not exist');

See the full working example here.

Tested in:
jQuery version 1.7.2
FF 3.6+, Safari, Chrome, IE8+
iOS

Great books are available on JavaScript here.

This entry was posted in JavaScript and tagged .
Bookmark the permalink.

One Response to JavaScript logging in console and window

  1. Pingback: Banner ad animation with jQuery | The Blog of Clarke Ulmer

Leave a Reply

Your email address will not be published. Required fields are marked *

*

To include code in your comment: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag(or shortcode) and format it when submitted.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">