Javascript Console Logging Function

Posted: March 18, 2012 in Javascript
Tags: , , ,

I use this function to send debugging information to the console in Firefox and IEDeveloper.

Using a query string parameter [debug=1] to enable logging, to save some cycles when its not needed.

I am sure there are better ways to write this…


// Logging Function
function log(msg){
if(!getParameterByName("debug") == 1) { return; }
if (window.console && console.log) {
console.log(msg); // Valid in Firebug and IEDeveloper
}
}

To get the query string parameter, I am using a snippet found on the net a while back. I would give credit, but I have no idea where the original source was.


function getParameterByName( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}

Advertisements

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