Disable a link from going to the href URL with jQuery

When assigning a click handler to an anchor tag with jQuery you'll often want to then prevent the browser from clicking through to the actual link. There are two ways I know of to do this with jQuery and these are presented there.

Note that the return false line needs to be the last line in the function otherwise any code below it in the function will not execute. You could also return the value based on some condition e.g. return someVariable == 'foobar' - if it returns true then the link will be followed; if false it won't.

Prevent the default action with preventDefault()

The final example below will again show the alert dialog and will not follow the link.

Unlike the return false example, e.preventDefault() can be anywhere in the function and the rest of the code in the function will still be executed. I've put it at the start of the function but it could just as easily be at the end or some middle point.