5

I'm working on a new project which has some complex javascript. I can't post any code so that's not what my question is about.

I have a script which works in Firefox 3.0. It was pointed out that the script did not work in Firefox 3.5, so I'm trying to make it work. Indeed the script didn't produce the expected results, so I installed the latest version of Firebug, enabled the console and refreshed the page.

And wow, it worked.

No errors, warnings nothing.

So I disabled the console, and then it didn't work anymore...

What's going on here? The Firebug console somehow changes something in Firefox that makes my script work? Any advice on what next? (besides asking future visitors to install Firebug...)

Beska
  • 12,445
  • 14
  • 77
  • 112
Ropstah
  • 17,538
  • 24
  • 120
  • 194

4 Answers4

10

Could it be something as simple as forgetting to comment a call to console.log() somewhere in your javascript?

If you have hanging references, and the user doesn't have Firebug installed, you're going to get a runtime error that will halt execution of the script.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Yeah this sounds pretty likely too. I thought about it for a second but assumed since you didn't have firebug installed at first it wouldn't be there, but after reading this answer just realized there's a good chance if you copied and pasted code some debugging was sitting there. +1 – NateDSaint Sep 11 '09 at 19:53
  • I dunno, I have to give the benefit of the doubt to the developer that he would be able to recognize such an obvious error. – Zoidberg Sep 11 '09 at 19:58
  • I'm reading it and i'm feeling so stupid at the same time :). However Mushex what first answering... – Ropstah Sep 11 '09 at 20:00
  • This has happened to me as well. `console.log` will break everything if `console` isn't available, which it in most browsers. – August Lilleaas Sep 11 '09 at 20:01
7

It sounds to me like there's a chance you have a threading problem, and FireBug is analyzing and possibly slowing down one of the threads so that it has time to complete before the next step is resolved.

Are you possibly utilizing ajax, and something is waiting on that response? Or possibly you're doing something on or after the load of an object that is depending on something else in the DOM?

UPDATE: For those stumbling upon this now, "threads" in JavaScript really only exist in abstraction (web workers, etc). I was mis-using the term. I was really thinking of an asynchronous action that returned before another one was ready.

NateDSaint
  • 1,524
  • 2
  • 16
  • 35
  • This would make sense since firefox's javascript engine seems to get faster with every release. – Zoidberg Sep 11 '09 at 19:45
  • I am using ajax yes, i think however Justin is right about the console.log() :) – Ropstah Sep 11 '09 at 19:58
  • It's a good idea to check on that Ajax while you're at it. I had a similar issue once using AJAX with Google Maps, where I alerted what a variable was, and that somehow magically fixed the code. Multiple Bald Spots Later, it turned out while the alert window was up, freezing the next step, it had time to load and fixed my threading issue. – NateDSaint Sep 11 '09 at 20:00
  • Javascript doesn't have threads, does it? You can have asynchronous code that'll be executed at a later point in time, but it's still not multi-threaded. – August Lilleaas Sep 11 '09 at 20:00
  • Javascript doesn't utilize "threads" in the traditional sense unless you specify them via extraction outside of the xhmtlrequest, via http://ajaxian.com/archives/multi-threaded-javascript but the asynchronous loading issues are similar in concept. So more specifically, a "timing" issue, in this case perhaps. – NateDSaint Sep 11 '09 at 20:06
6

Check in your code for console.log(), console.debug().Calling window.console objects methods throws an error if console is undefined (as expected).

In most cases you can easily delete or comment that lines.

Mushegh A.
  • 1,431
  • 12
  • 18
  • @ropstah Not that it's important...but check your math. This is the last answer, not the first. – Justin Niessner Sep 11 '09 at 20:17
  • Whoops, you are right, i was indeed under influence while reading "8 minutes ago" by Mushex and your "9 minutes ago" Justin... :) – Ropstah Sep 12 '09 at 09:00
  • Another good idea is to include firebugx.js in your javascript, it'll add a fake console object to the window that ignores all your console.log calls. – Dobes Vandermeer Oct 27 '11 at 02:43
0

I wrote a simple wrapper for firebug (I just use debug but it should give you what you need to duplicate the other methods) that only writes when the console is there so I can use firebug, don't need to go comment out my debug statements and it doesn't break sites for people without it.

If you use this code then use fbconsole.debug instead of console.debug you will never have this problem:

function fbconsole () {
    this.debug = function (val) {
        if(typeof(console) !== 'undefined' && console != null) {
            console.debug(val);
            }
        }
    }
var fbconsole = new fbconsole();