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, " "));
}

Leave a comment