views:

60

answers:

3

Hello, 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

+6  A: 

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.

SBUJOLD
preventDefault() is not supported by IE
Dean
Where do you see that ? Are you talking about IE's default Event object ? Because jQuery's Event::preventDefault() has been around since version 1 and normalized accross all browsers. Here's an example of it that works for me in IE7 http://jsbin.com/olefe3/2
SBUJOLD
Thanx, Il check that out, sry I didnt realize yu were talking about jQuery's :preventDefault()
Dean
+4  A: 

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;
    })
}

?

Skilldrick
Yes, then it calls processSlideReq function, which in turn calls a function that animates stuff. Untill it finishes running those functions, the browser will follow the link.
Dean
@Dean - are you sure? Nothing should happen until the click handler returns.
Skilldrick
Oh sorry sorry, the function was unfinished. Thanx it works fine :)
Dean
@Dean - functions that animate stuff in jQuery return immediately. Can you show an example of `return false` not working?
Skilldrick
@Dean - Ok, ignore my last comment then :)
Skilldrick
+1  A: 

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();
mattbasta