Dean
Dean

Reputation: 8055

JavaScript: how to return false to link without breaking function?

I have a question (surprisingly ;) ):

How can I return false to a link without breaking the function? I did this previously without jQuery, back then I set an animation to execute after an interval and returned false. This time I need the function to continue running.

function initNavLinks(){
    navLinks = $("div#header a").click(function(){
        return false;
        processSlideReq();
    })
}

Thanx in advance

Upvotes: 1

Views: 355

Answers (3)

mattbasta
mattbasta

Reputation: 13709

Just as a theoretical curiosity, you could also use JavaScript 1.7's generator support (though it only works in Firefox and wouldn't apply for links).

function x() {
    yield false;
    yield doSomething();
}

first_return_value = x();
second_return_value = x();

Upvotes: 1

SBUJOLD
SBUJOLD

Reputation: 1473

You probably should just prevent the event's default action like this:

function initNavLinks(){
    navLinks = $("div#header a").click(function(e){
        e.preventDefault();
        processSlideReq();
    })
}

You may want to read jQuery's Event Object documentation and bind documention to understand the differences between of returning false, calling preventDefault() and calling stopPropogation() within event handlers.

Upvotes: 6

Skilldrick
Skilldrick

Reputation: 70819

Is there any reason why you can't just swap the lines around and do:

function initNavLinks(){
    navLinks = $("div#header a").click(function(){
        processSlideReq();
        return false;
    })
}

?

Upvotes: 4

Related Questions