blue-sky
blue-sky

Reputation: 53876

Why return a boolean if not being used?

I've encountered multiple functions where the developer is returning a boolean, even though this boolean is not checked :

function  getPage()
{        
    return false;
}

<a onclick="getPage();"><img src="/myimage.gif"/>

This developer has since moved, why return a boolean in this case ?

Upvotes: 1

Views: 81

Answers (1)

WTK
WTK

Reputation: 16971

He's returning false there, because otherwise browser would follow the clicked link. By returning false, the click event is effectively "canceled". It's equivalent of preventDefault on event object.

It doesn't work in this case though, the intention probably was to do:

<a onclick="return getPage();"><img src="/myimage.gif"/>

Upvotes: 7

Related Questions