When debugging client side javascript today in Firefox, I came across something that I found rather strange and a bit unnerving. Also, I was not able to duplicate this behavior when debugging the same script using IE / VS2010.
I created a simple html document example to illustrate abnormally what I see.
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" type="text/javascript" ></script> </head> <body id="main_body"> <script type="text/javascript"> $(function () { $(".test-trigger").on("click", function () { loadStuff(); console && console.log && console.log("this will probably happen first."); }); }); function loadStuff() { $.get("http://google.com/") .fail(function () { console && console.log && console.log("this will probably happen second."); }); } </script> <button class="test-trigger">test</button> </body> </html>
If you upload this document to Firefox (I use version 13.0 with Firebug version 1.10.1 in Windows 7), click the "Check" button and look at the console tab in Firebug, you should notice that the request for receipt failed (cross domain violation, has nothing to do with the point I'm trying to make here), and then you will most likely see:
this will probably happen first. this will probably happen second.
Now place breakpoints on lines 13 and 20:
13: console && console.log && console.log("this will probably happen first."); 20: console && console.log && console.log("this will probably happen second.");
If you hit the test again, you will break line 13 as expected. Now resume execution. If your experience is similar to mine, you will not break on line 20. Also, if you go to the console tab, you will see the following sequence of log output:
this will probably happen second. this will probably happen first.
For me, this means that the ajax request handler fails in a thread other than the one in which the click handler is executed. I always had to believe that all javascript for one page will be executed by one thread in any browser. Did I miss something really obvious here? Thanks for any understanding of this observation.
Oh, if I debug the same page as in IE using Visual Studio, both breakpoints fall as I expected.