You need to sign in to do that
Don't have an account?

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
<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
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
Thanks for the quick response. Makes sense.
Could I have used the preventDefault function as another option ?
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
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).
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.