11

Let's say I have a link:

<a href='http://example.com/file.zip' id='dl'>Download</a>

And while someone click on it, I want to do something, such as:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500);
});

Is there a way after I e.preventDefault, to go ahead and doDefault? something like:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500);
    e.doDefault();
});

so it'll run the event normally?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Itai Sagi
  • 5,537
  • 12
  • 49
  • 73

3 Answers3

11

You can only prevent the default behavior (with e.preventDefault()).

Now if you expect the link to be navigated when the .fadeOut() method has finished, you should prevent the default behavior and use the callback parameter of the fadeOut method to navigate when the animation has finished:

$("#dl").live("click", function(e){
    e.preventDefault();
    var href = this.href;
    $("#dlbox").fadeOut(500, function() {
        window.location = href;
    });
});

DEMO

Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
  • 1
    fadeOut is asynchronous, you'd have to put code into the callback method of fadeout for it to work like that – NibblyPig Mar 05 '12 at 12:55
  • you're assuming link, what if that's a form submission? – Itai Sagi Mar 05 '12 at 13:01
  • true that :) was shooting for a general thing there – Itai Sagi Mar 05 '12 at 13:03
  • .live() has been deprecated as of 1.7, so you should use .on() or .click() instead. http://stackoverflow.com/questions/8752321/jquery-live-vs-on-method-for-adding-a-click-event-after-loading-dynamic-ht – Luke Oct 28 '13 at 15:39
4

There is no doDefault() command, you need to re-architect your code.

I would prevent default, then perform the url direct yourself via window.location. Possibly use a different tag to an anchor tag too - by overriding its functionality it makes no sense to use it in the first place.

eg something like this:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500, function() { window.location='www.fish.com'; });
});
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
-1

Maybe this post can help you? You can also just return false to prevent the link to go to the location in the href.

Community
  • 1
  • 1
Bas Slagter
  • 9,831
  • 7
  • 47
  • 78