apex:actionFunction

A component that provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component.

Unlike <apex:actionSupport>, which only provides support for invoking controller action methods from other Visualforce components, <apex:actionFunction> defines a new JavaScript function which can then be called from within a block of JavaScript code.

Note: Beginning with API version 23 you can't place <apex:actionFunction> inside an iteration component — <apex:pageBlockTable>, <apex:repeat>, and so on. Put the <apex:actionFunction> after the iteration component, and inside the iteration put a normal JavaScript function that calls it.

Example

<!-- Page: -->
                        
<apex:page controller="exampleCon"> <apex:form> <!-- Define the JavaScript function sayHello-->
        <apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out" status="myStatus"/> </apex:form> <apex:outputPanel id="out"> <apex:outputText value="Hello "/> <apex:actionStatus startText="requesting..." id="myStatus"> <apex:facet name="stop">{!username}</apex:facet> </apex:actionStatus> </apex:outputPanel> <!-- Call the sayHello JavaScript function using a script element-->
    <script>window.setTimeout(sayHello,2000)</script> <p><apex:outputText value="Clicked? {!state}" id="showstate" /></p> <!-- Add the onclick event listener to a panel. When clicked, the panel triggers the methodOneInJavascript actionFunction with a param -->
    <apex:outputPanel onclick="methodOneInJavascript('Yes!')" styleClass="btn"> Click Me </apex:outputPanel> <apex:form> <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate"> <apex:param name="firstParam" assignTo="{!state}" value="" /> </apex:actionFunction> </apex:form> </apex:page> /*** Controller ***/
public class exampleCon { String uname; public String getUsername() { return uname; } public PageReference sayHello() { uname = UserInfo.getName(); return null; } public void setState(String n) { state = n; } public String getState() { return state; } public PageReference methodOne() { return null; } private String state = 'no'; }

Attributes

Attribute Name Attribute Type Description Required? API Version Access
action ApexPages.Action The action method invoked when the actionFunction is called by a DOM event elsewhere in the page markup. Use merge-field syntax to reference the method. For example, action="{!save}" references the save method in the controller. If an action is not specified, the page simply refreshes. 12.0 global
focus String The ID of the component that is in focus after the AJAX request completes. 12.0 global
id String An identifier that allows the actionFunction component to be referenced by other components in the page. 12.0 global
immediate Boolean A Boolean value that specifies whether the action associated with this component should happen immediately, without processing any validation rules associated with the fields on the page. If set to true, the action happens immediately and validation rules are skipped. If not specified, this value defaults to false. 12.0 global
name String The name of the JavaScript function that, when invoked elsewhere in the page markup, causes the method specified by the action attribute to execute. When the action method completes, the components specified by the reRender attribute are refreshed. Yes 12.0 global
namespace String The namespace to use for the generated JavaScript function. The namespace attribute must be a simple string, beginning with a letter, and consisting of only letters, numbers, or the underscore ("_") character. For example, "MyOrg" and "Your_App_Name_v2" are supported as namespaces. If not set, no namespace is added to the JavaScript functions generated by <apex:actionFunction>, preserving existing behavior. 12.0 global
onbeforedomupdate String The JavaScript invoked when the onbeforedomupdate event occurs--that is, when the AJAX request has been processed, but before the browser's DOM is updated. 12.0 global
oncomplete String The JavaScript invoked when the result of an AJAX update request completes on the client. 12.0 global
rendered Boolean A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true. 12.0 global
reRender Object The ID of one or more components that are redrawn when the result of the action method returns to the client. This value can be a single ID, a comma-separated list of IDs, or a merge field expression for a list or collection of IDs. 12.0 global
status String The ID of an associated component that displays the status of an AJAX update request. See the actionStatus component. 12.0 global
timeout Integer The amount of time (in milliseconds) before an AJAX update request should time out. 12.0 global
© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.