{"id":108,"date":"2008-11-26T09:12:52","date_gmt":"2008-11-26T14:12:52","guid":{"rendered":"http:\/\/www.timrosenblatt.com\/blog\/2008\/11\/26\/a-javascript-debugging-tip-for-firebug\/"},"modified":"2008-12-04T22:41:31","modified_gmt":"2008-12-05T03:41:31","slug":"a-javascript-debugging-tip-for-firebug","status":"publish","type":"post","link":"http:\/\/www.timrosenblatt.com\/blog\/2008\/11\/26\/a-javascript-debugging-tip-for-firebug\/","title":{"rendered":"A Javascript debugging tip for Firebug (or &#8220;Stop using alert()!&#8221;)"},"content":{"rendered":"<p>Did you know that if you&#8217;ve got Firebug for Firefox installed, you can use it for debugging your own code? By calling<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">console.log(&quot;Here's a message!&quot;);<\/pre>\n<p>Firebug will print the message to it&#8217;s internal message log. Neat!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/images\/firebug-message-console.png\" alt=\"Firebug message console\" border=\"1\" height=\"162\" width=\"436\" \/><\/p>\n<p>Now, that&#8217;s all good. But let&#8217;s say you&#8217;re on a project that&#8217;s not using a Javascript preprocessor to minify and strip debugging code &#8212; which would be the best option. You want the benefits of debugging, but not having to constantly remove debugging code for deployment. If a user doesn&#8217;t have Firebug installed, they won&#8217;t have a console object. So obviously leaving your debug code in is going to cause an error.<\/p>\n<p>Or will it?<\/p>\n<p>Try adding this code before any of your other Javascript. It will set up a fake console so that if Firebug is not installed, there won&#8217;t be any errors.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">if(!console || !console.log) {\r\nvar console = new Array();\r\nconsole.log = function () {}\r\n}<\/pre>\n<p>Bonus idea! If you combine the idea shown here, with my <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/07\/23\/firefox-extension-debugging\/\">previous post about debugging Firefox extensions<\/a>, you can see how to prevent extension debugging from getting in the way of your users.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">if(!Firebug || !Firebug.Console || !Firebug.Console.log) {\r\nvar Firebug = new Array();\r\nFirebug.Console = new Array();\r\nFirebug.Console.log = function () {}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Did you know that if you&#8217;ve got Firebug for Firefox installed, you can use it for debugging your own code? By calling Firebug will print the message to it&#8217;s internal message log. Neat! Now, that&#8217;s all good. But let&#8217;s say you&#8217;re on a project that&#8217;s not using a Javascript preprocessor to minify and strip debugging &hellip; <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/11\/26\/a-javascript-debugging-tip-for-firebug\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Javascript debugging tip for Firebug (or &#8220;Stop using alert()!&#8221;)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145,236,88,123],"tags":[193,192,346,191,57,579],"_links":{"self":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/108"}],"collection":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":0,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}