views:

999

answers:

4

I'm trying to prevent A HREF from actually opening the link, but to execute a script. I got the script part working, but it seems Firefox would just ignore this:

$("#permalink a").click(function(id){
  $("#newPost").fadeToggle;
  event.preventDefault();
  var id = this.getAttribute('href');
  $("#newPostContent").load(id);
  $("#newPost").show("fast");
});

Can anyone suggest a cross-browser script for preventing defaults?

+6  A: 

The standard solution to this is to return false from the click() handler. return false is the equivalent of calling preventDefault() and stopPropagation() on the event. preventDefault() is sufficient for this task too so either it or return false will work.

Your problem seems to be syntax errors and misnamed parameters. I see:

  • fadeToggle has no parentheses;
  • the function parameter is called id yet you call event.preventDefault(); and
  • You're fading the post and then immediately showing it? That'll queue two animations but doesn't make a lot of sense. You probably want to chain together those two things and the load() in a rational manner.

So this should work:

$("#permalink a").click(function(event) {
  $("#newPost").fadeToggle();
  var id = this.getAttribute('href');
  $("#newPostContent").load(id);
  $("#newPost").show("fast");
  return false;
});

You can freely replace return false with event.preventDefault() in this instance but the propagating click() event might or might cause other issues depending on what other event handlers you have.

cletus
Thank you for your help.
konzepz
"Hey what's going on on this side?"
Steerpike
That's not the problem here. The problem is .click(function(id){ should be .click(function(event){.
karim79
"But whatever the case, preventing link navigation is best done by return false from the click event handler." - actually, it's not - that screws up if you require events to propagate properly. It's a *lazier* way to do it, requiring less code, but preventDefault does precisely what is required (no default action) and nothing more.
ijw
Why is this getting downvoted?
cletus
+1  A: 

You should just be able to return false;

Steerpike
"Back you go answer, to waits for for a questioner with less discerning tastes.'
Steerpike
A: 

Add return false into the function, it should help.

$("#permalink a").click(function(id){
                    $("#newPost").fadeToggle;
                event.preventDefault();
                    var id = this.getAttribute('href');
                    $("#newPostContent").load(id);
                $("#newPost").show("fast");
                return false;
            });
martin.malek
+3  A: 

Er...

            $("#permalink a").click(function(id){

should be

            $("#permalink a").click(function(event){

surely?

... then

var id=event.target;

will give you the object you're working on, and event.preventDefault() will stop the default action.

Typically I return false; myself, but this both prevents the default action and stops propagation of the event, which might have effects you didn't expect with other event handlers.

ijw
... actually, I note that you pass 'id' in but it's overridden by your attribute-fetch. Better still, the 'id' variable is not an id when passed in (it's an event obj though I assumed you thought it was the DOM object) nor when you get the attr (then, it's a URI). That plus the missing brackets on your fadeToggle call says to me that you need to read the code you've written a bit more carefully before you start pointing fingers at the jQuery library.
ijw