2

Possible Duplicate:
Jquery if conditions false then prevent default

I have this code, it's running fine but I want it to be dynamic.

$(".nav a").click(function () {     
    var getid = $(this).attr('id');
    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow");         
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow", function () {
        window.location = getid+".php";
    });
});

I want to execute the fade out first before directing it using the href.

What I want is this first

$(".nav a").click(function () {     
    var getid = $(this).attr('id');
    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow"); 
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow");
});

then trigger this after

.nav a = href = "link"
Community
  • 1
  • 1

4 Answers4

3

You need to use event.preventDefault() first to stop the link behaviour, then manually redirect the user. Try this:

$(".nav a").click(function (e) {     
    e.preventDefault();

    var getid = $(this).attr('id');
    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow");         
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow", function () {
        location.assign = getid + ".php";
    });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

Try This:

$(".nav a").click(function (e) {     

    e.preventDefault(); /*  If this method is called, the default action of the event will not be triggered. e.g clicked anchors will not take the browser to a new URL */

    var pageUrl = this.id + ".php";

    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow"); 
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow", function () {
        window.location.replace(pageUrl);
    });

});
Ahsan Khurshid
  • 9,383
  • 1
  • 33
  • 51
0

Give this a try:

$(".nav a").click(function (e) {   
    e.preventDefault();
    var $ele = $(this);
    $("#background-wrapper, #page_box, header, footer").fadeOut("slow", function () {
        window.location = $ele.attr('id')+".php";
    });
});
Ian Wood
  • 6,515
  • 5
  • 34
  • 73
0

Perhaps the following will work for you?

$(".nav a").click(function (e) {     
    e.preventDefault();
    var pageUri = $(this).attr('id') + '.php';
    $("#background-wrapper, #page_box, header, footer").fadeOut('slow', function () {
        window.replace(pageUri)
    });
}
Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81