Your browser is very old. You might enjoy surfing the web more if you used something newer like:

Google Chrome

Even Firefox would be OK.

If you're being forced at gunpoint to use Internet Explorer, you should at least upgrade it. Version 8 is tolerable and 9 will be OK when it comes out.

Posts from June 2010

Catching Ajax errors with jQuery

I had to debug an issue this morning where an Ajax call ($.getJSON) failed in every browser except Firefox.

Some googling lead to this answer on Stackoverflow which describes how to setup error handling for ALL Ajax calls in your code.

Like most things with jQuery, it’s very simple:

$(document).ready( function() {
        $.ajaxSetup( {
            'error':  function(XMLHttpRequest, textStatus, errorThrown) {
                alert('Ajax Error:  (' + textStatus + ') ' + errorThrown);
            }
        });
);

If you read that answer, you’ll notice he suggests malformed JSON as the most likely culprit for the callback to $.getJSON not firing.

I added the above error handler and, sure enough, it was a bad-JSON error. I ran my JSON through JSONLint to discover an error that I’ve run into many times and will, hopefully, someday, remember to look for.

  [
    {"ant":"bear"},
    {"cat":"dog"},
  ]

See that comma after “dog”}? Firefox is pretty forgiving about that sort of thing but Internet Explorer (and Chrome) don’t like it. I’m constantly forgetting about removing that trailing comma, I suspect because that’s perfectly valid when declaring arrays and dictionaries in Python.

Oh well. And yay for jQuery and JSONLint.

Fun with Vim movement commands

If you’ve used Vim at all, you’re probably already familiar with the basic character movement commands

h (left), j (down), k (up), l (right)

or word movement commands

w (forward), W (same as w), b (backward), B (same as b)

You may or may not, however, have experienced the sheer joy of using

fx (Find character forward on the current line), Fx (Same as f but moves backwards)

and

tx (Find character forward BEFORE x on the current line), Tx (Same as t but moves backwards)

The above character searching commands are especially useful when combined with the delete (d) command.

Let’s say you have some text like the following and the cursor is at the beginning of the line. Let’s also say that we want to change “This is a big bunch of text.” to something else.

First picture

Sure, we could move over there character-by-character (or even go to the end of the line and move back) and the backspace a bunch of times. Or we could use the mouse to highlight and cut. But there’s a better way.

With just a couple of keystrokes (f>l), we can move the first character AFTER the “>” that closes the first part of the <a> tag.

Second picture

Let’s break that down:

  1. We move to the first occurrence of the > character on the line (f>).
  2. We move one character to the right (l)

Then we can quickly delete “This is a big bunch of text.” by combining d with t to delete everything up to BEFORE the next occurrence of <.

Like so:

dt<

Third picture

You can also leverage the power of doing a command multiple times. In next example, we still want to change the link text but now the text is surrounded by <span> tags that we also want to delete.

Fourth picture

To do this, we have to make a small change to our delete command (I’m assuming the cursor is already in the correct place):

d2t<

See that 2? That’s repeating the t command twice. So we’re telling Vim to delete everything up to BEFORE the SECOND occurrence of <. This ensures that the <span> tag is also deleted.

These are just a few basic examples of movement commands, and combining deletes with movements and repetition. There are lots of other commands that I haven’t covered and every one you learn will make you that much faster.