How To Debug Web Applications With Firefox

Debugging is one of the most painful parts of developing web apps. You have to deal with browser inconsistencies with HTML, CSS and javascript, let alone the difficulty of debugging javascript itself.

Here’s a rundown of the Firefox extensions I use to manage this madness.

Taming CSS: Web Developer Toolbar

Install Web Developer Toolbar. Just do it.

Debugging CSS can be really frustrating. The Web Developer Toolbar lets you inspect and edit (in real-time) the HTML and CSS of your page, so you can see what’s happening when things don’t line up. It can do a heck of a lot more, but here’s what I use it for:

Ctrl + Shift + F: Display element information. This puts a red box under your mouse. Move the mouse over an element and its attributes appear in a pop-up: the name, class, pixel sizes, fonts, everything. Here’s what you can do:

  • Figure out what classes are creating the styles you see
  • Easily get the div’s id for use with Firebug (below)
  • Figure out how big an image is (pixel height and width)

web_developer_google.png

Ctrl + Shift + E: Edit CSS. This pops open a sidebar tab with the current stylesheets. You can edit any attributes and see the effect in real-time (like giving Google a black background):

web_developer_google_black.png

My favorite CSS style is border: 1px solid red;

I’ve done the following hundreds of times during the course of web development:

  • Find a div with your mouse (ctrl + shift + f)
  • Get its id
  • Edit CSS (ctrl + shift + e)
  • Put a border on the div: #mydiv{border: 1px solid red;}
  • Play with widths, heights, margins and paddings until it lines up nicely
  • Remove the border

But rather than deleting the border, put an “x” in front: “xborder: 1px solid red”. The CSS won’t be valid so the border is ignored, but keeps the style around in case you want to enable it later.

Select all the text in your edited CSS file and paste it into the real CSS file. Bam, your changes are now live. It’s almost the reverse of creating a file in DreamWeaver and viewing it in Firefox. You are viewing the live file in Firefox, making changes, and copying those back into your text editor. I’ve found this very effective for editing CSS, you avoid the constant back-and-forth switching because Firefox now has a CSS editor.

Bonus: ColorZilla Picks Colors

ColorZilla gives you a dropper that can find the hex RGB value (#123456) of anything on the page. This is great when designing, and you want to match a font color to a color in your page. This is way faster than taking a screenshot and opening it up in Photoshop.

Keeping Javascript In Line: Firebug

Firebug, how I love thee. If you love yourself you will install it immediately and save countless hours of frustration.

Firebug can debug javascript, examine the DOM, and do much more (you can and should read all about it). Here’s how I use it:

F12: Open Firebug. You may have to enable it for the page.

Console Tab: Write quick javascript commands — it even has autocomplete on variable names and properties. Play around with your functions, change CSS attributes, add elements to the page — whatever it takes to test.

Script Tab (Debugging): Best. Feature. Ever. Click on a line number to set a breakpoint (red dot) in your javascript. Reload the page and it will break (pause) when it encounters the line.

At this point, you can switch over to the console to examine and change variables, and figure out what the heck is going on when your code won’t work. You can then hit the blue “play” button and continue running your app, until the next breakpoint.

Net Tab: Find the download performance of your page.

Profile Button (on Console Tab): Find the run-time performance of your page. Click “profile” to begin capturing information, do some commands, and then click stop. You’ll get a report of where your code spends its time. If you must optimize, optimize the common-case first.

If you are a more visual person, try this awesome collage:

firebug_awesome_collage.png

Not satisfied? Check out the examples on the home page.

Dive into the details: Live HTTP Headers

Sometimes you need to dive into the nitty-gritty. What cache headers is my site sending back? Are my pages really gzip-encoded?

I know these questions keep you up at night, so here’s what you can do:

1. Install Live HTTP Headers
2. Open it (Tools > Live HTTP Headers)
3. Visit a page / press refresh
4. Rejoice

live_headers.png

As you visit a page, you’ll see HTTP headers fly by as your browser requests elements. If items are cached, the browser may not request them at all (awesome!) or may request the element and get a 304 “Not Modified” response (slightly less awesome, you still had to check with the server). I’ve written more on cache behavior, and Live HTTP Headers is a great way to learn about HTTP caches (something every webdev should be interested in for performance reasons).

Even better, you can “replay” any header, editing the data that is sent. This is useful when testing or debugging cache or gzip encoding behavior.

Debugging IE: The lost chapter

Argh, unfortunately IE lacks these wonderful tools. There is a script debugger, but it doesn’t hold a candle to Firebug. In fact, I often just resort to alert statements, which make you shudder after being spoiled by Firebug.

One less painful method I use is this:


In your HTML: <div id="log"></div>

In your Javascript:

function log(str){
  var log = document.getElementById("log")
  if (log){ // let's be safe...
	 log.innerHTML += str + "<br/>";
  }
}

Usage: log("Hi there!");

Optional: create an eval box:
<input name="eval" id="eval"/>
<a href="javascript:void(0);" onClick="log(eval(document.getElementById('eval').value));">go</a>

It’s nothing fancy, just a simple logging function that appends text to a div. Yes, it’s brutal, but it’s better than alert() statements, especially if you have a loop (unless you like repetitive stress injuries or want to condition yourself to fear dialog boxes). If anyone knows a good way to debug javascript in IE I’d love to know. The tools I’ve tried have been very clumsy and disjoint, taking you out of the browser.

I try to do 95% of my development in Firefox, and debug IE-specific issues (like erratic substr behavior) using this method.

Keep Getting Better

Web Developer Toolbar and Firebug can do way more than I’ve described here. Like the 80/20 rule, these are commands I use most frequently that give me the best bang for my buck. Take a few minutes to learn these tools and you’ll save hours down the line. And here’s a few more tools for web development.

These tools might not save you from getting a nervous twitch in one eye from building web apps, and that’s ok. They’ll save you from getting that twitch in both.




Get the Math, BetterExplained ebook

Like the site? Take it with you. Develop your math sense using insights, not memorization.

30 Comments »

Trackbacks & Pingbacks

  1. Pingback by Debugging Web Applications with Firefox — March 21, 2007 @ 1:36 pm

  2. Pingback by FireFox Hacker - How to Debug Web Apps With Firefox — March 21, 2007 @ 7:51 pm

  3. Pingback by How to Optimize Your Site with GZIP Compression | BetterExplained — April 4, 2007 @ 11:11 pm

  4. Pingback by How To Optimize Your Site With HTTP Caching (Part 1) | BetterExplained — April 7, 2007 @ 2:27 am

  5. Pingback by Debugging again » Code Candies — April 30, 2007 @ 12:18 am

  6. Pingback by Starting Ruby on Rails: What I Wish I Knew | BetterExplained — June 19, 2007 @ 9:16 pm

  7. Pingback by ersin dogan Herkesin bakmadığı yönden bak dünyaya <<Mevlana>> » Sitenizi Gzip sıkıştırması ile nasıl hızlandırabilirsiniz ? — July 27, 2007 @ 4:38 am

  8. Pingback by iCode Inc — December 28, 2007 @ 10:32 pm

  9. Pingback by Speed Up Your Javascript Load Time | BetterExplained — March 13, 2008 @ 8:44 pm

  10. Pingback by html-utvikler » Blog Archive » Debug nettsidene med Firefox — April 9, 2008 @ 1:04 pm

  11. Pingback by How To Make a Bookmarklet For Your Web Application | BetterExplained — April 16, 2008 @ 10:39 pm

  12. Pingback by How To Optimize Your Site With GZIP Compression « SEO exploration — May 23, 2009 @ 7:18 am


Comments

  1. Fiddler (http://www.fiddlertool.com/fiddler/) is a good replacement for Live HTTP Headers, when you have to use IE.

    Graham King — March 22, 2007 @ 8:21 am

  2. Thanks Graham — I’ll check it out!

    Kalid — March 22, 2007 @ 11:47 am

  3. The same things you do in Web Developer toolbar can be done in Firebug with even more convenience. So why not use one great tool for the job instead of such separation?

    Mourner — March 22, 2007 @ 2:30 pm

  4. I find Firebug is great for debugging javascript, but Web Developer toolbar is better for designing your site. It has awesome tools for showing/hiding images, measuring pixel distances onscreen, validating CSS/HTML, resizing your browser, viewing the HTML source of iframes and more.

    Kalid — March 22, 2007 @ 7:20 pm

  5. I can also vouch for Fiddler. I use it since I use IE and find it invaluable.

    Trevin — March 25, 2007 @ 3:42 pm

  6. This is a great guide

    Funky — September 2, 2007 @ 10:56 am

  7. Thanks Funky, glad you liked it.

    Kalid — September 2, 2007 @ 10:37 pm

  8. There is something like web developer toolbar for IE called Internet Explorer Developer Toolbar: http://www.microsoft.com/downloads/details.aspx?familyid=e59c3964-672d-4511-bb3e-2d5e1db91038&displaylang=en

    It’s not bad.

    Tim — October 5, 2007 @ 11:22 am

  9. Hi Tim, thanks for the tip.

    Kalid — October 5, 2007 @ 12:44 pm

  10. IMHO, Microsoft Script Editor is at least as good as firebug for IE script debugging. For those who have ms office installed, you may find it as mse7.exe under you ms office folder, so it’s almost free.
    Visual studio is also pretty handy as a js debugger for IE.

    Jay — December 9, 2007 @ 8:10 pm

  11. I was debugging javascript using alerts and div log (similar to yours) until now. Thanks for sharing!

    collector — January 22, 2008 @ 9:25 am

  12. Thanks, glad you liked it.

    Kalid — January 22, 2008 @ 10:51 pm

  13. I’d disagree with Jay – I use both Firebug and Microsoft Script Editor, and MSE is not nearly as useful in my experience. MSE frequently barfs on dynamically loaded js scripts or on anonymous functions.

    Tyler Style — May 22, 2008 @ 6:00 pm

  14. +1 for the IE Developer Toolbar, it’s actually more like Firebug (it even looks similar).

    toupeira — May 26, 2008 @ 11:48 am

  15. Cheers for this excellent reference. For example, I’ve had Firebug and Web Dev Toolbar installed for ages, but didn’t know about those keyboard shortcuts.

    Winooski — June 18, 2008 @ 8:33 am

  16. Hi,

    Thanks for posting such a useful article. It would benefit many neophytes as well as experienced web developers.

    saurabh — November 26, 2008 @ 4:34 am

  17. I will love this tool! I find your website very helpfully, thanks and keep going on, for us!

    Notizie — December 13, 2008 @ 12:07 pm

  18. Very Great Tool & Great Site.
    It is all explained in much easy to understand way.

    Joomla Developer — July 3, 2009 @ 6:27 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Have a question? Know an explanation that caused your own a-ha moment? Write about it here.




Like it? Try All articles, RSS Feed or Email Subscription | Idea or suggestion? Contact me
copyright © 2009 Kalid Azad