function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris VogeChris Voge 

What does "return false" do exactly ?

Here is the sample code from the getEnclosingPrimaryId function documentation:

<apex:page standardController="Case">
    <A HREF="#" onClick="testCloseTab();return false">
         Click here to close this primary tab</A>

    <apex:includeScript value="/support/console/20.0/integration.js"/>
    <script type="text/javascript">
        function testCloseTab() {
            //First find the ID of the current primary tab to close it
            sforce.console.getEnclosingPrimaryTabId(closeSubtab);
        }
       
        var closeSubtab = function closeSubtab(result) {
            //Now that we have the primary tab ID, we can close it
            var tabId = result.id;
            sforce.console.closeTab(tabId);
        };
    </script>
</apex:page>

Simple question: What does "return false" do ?

I've been working on an issue to open a subtab inside a primary tab by
clicking a apex commandButton. I have an onclick="openSubtabFunction()"  on
the button to trigger a function when clicked.

The issue was after the first click of the button, my subtab would not open.

Here is my action sequence:
- I click the commandbutton and the onclick function is triggered to open the subtab (tab opens fine)
- I close the subtab.
- I repeat step 1. Subtab doesn't open

All I did to fix the issue was change my onclick code to:

onclick="openSubtabFunction();return false"

And then, I can open the subtab repeatedly.

But, I just want to know what kind of difference "return false"
makes.

If anyone can explain, it would be appreciated very much.

Thanks, Chris

kaustav goswamikaustav goswami
A commandButton or a commandLink by their very nature tries to submit the form within which they are contained.

If there is an onclick event associated to these elements a sort of race condtion arises at times.

Clicking the button fires the onclick event and from that function call the control is taken else where.

Parallely the commandButton tries to execute the form submission.

When you mention the return false you forcefully prevent the control from returning to this unwanted form submission event. Thus avoiding the race.

That is all I know. I too would like to know this in more details.

Let me know if this helps.

Thanks,
Kaustav
Chris VogeChris Voge
Cool.

Thanks for the quick response. Makes sense.

Could I have used the preventDefault function as another option ?


kaustav goswamikaustav goswami
Actually a lot of that depends on the browser and the event that you are considering.

If you are using jQuery then returning false generally takes care of both the preventDefault and the stopPropagation().

However, from other scripting languages you may have to handle the propagation differently. It is actually related to how the browser takes up up the form submission event onclick of the link or button.

Thanks,
Kaustav
Avidev9Avidev9
There is good post on SE talking about hte same, you can look into it here http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false
Chris VogeChris Voge
Thanks, Avidev9. Will take a look.
bob_buzzardbob_buzzard
Return false tells the browser not to continue with the standard flow.  In the case where a command button has an onclick handler, returning false from that handler would stop the command button submitting the form.  A common use case for this is where some checks are carried out in the onclick handler that could identify errors - if there are errors, show an alert and return false and the form won't be posted back to the controller.

An example of where return false can help with race conditions is where a commandbutton has an onclick handler that invokes an apex:actionfunction to submit the form.  If you didn't have a return false, both the actionfunction and the commandbutton would attempt to submit the form, and there's no way to determine which would win.  By returning false from the onclick handler, the normal flow is stopped and the form is not submitted by the commandbutton.  The actionfunction submission then completes as normal as it is the only submission taking place.

In your first example piece of code, return false stops the click event from propagating up the DOM (by default, when you click on a link, all parent elements in the DOM also receive a click event).
jaysunjaysun
@Bob - you explained it quite well ; as usual.
by returning false on javascript function - you preserve Ajax effect - of action function otherwise the page will reload ( as if new submission) and view state will refresh.