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
Hardik ChavdaHardik Chavda 

How can I call multiple javascript function conditionally in oncomplete event in actionfunction?

<apex:actionFunction action="{!testMethod}" name="testFunction" oncomplete="{!if(var == true),onPageRerender();,endProcess();}" reRender="testId"/>
below is my action function in which I want to call multiple js function according to boolean variable which is in the controller. Is it possible? because it's not working, Can anyone help with this?
Best Answer chosen by Hardik Chavda
William TranWilliam Tran
Hardik, try to do the comparison in javascript instead of in the VF expression such as javascript expression:

if("{!var}"=='true') { onPageRerender(); } else{endProcess();}

Or you should consider calling a handler method all the time and have the handler method do the comparison and call the other functions.
 
<script>
function onCompleteHandler() {
  var successElement = document.getElementById('{!$Component.form.success}');
  if(successElement.value=='true') {
    onPageRerender();
  }
  else{endProcess();}

}
</script>

<apex:form id="form">
 ...
<apex:inputHidden id="success" value="{!var}"/>
<apex:actionfunction action="{!testMethod}" oncomplete="onCompleteHandler()" name="testFunction" reRender="testId"/>
</apex:form>

Note: I mocked this up without running it, so fix any syntax error before running it.

Thx

All Answers

William TranWilliam Tran
Hardik, try to do the comparison in javascript instead of in the VF expression such as javascript expression:

if("{!var}"=='true') { onPageRerender(); } else{endProcess();}

Or you should consider calling a handler method all the time and have the handler method do the comparison and call the other functions.
 
<script>
function onCompleteHandler() {
  var successElement = document.getElementById('{!$Component.form.success}');
  if(successElement.value=='true') {
    onPageRerender();
  }
  else{endProcess();}

}
</script>

<apex:form id="form">
 ...
<apex:inputHidden id="success" value="{!var}"/>
<apex:actionfunction action="{!testMethod}" oncomplete="onCompleteHandler()" name="testFunction" reRender="testId"/>
</apex:form>

Note: I mocked this up without running it, so fix any syntax error before running it.

Thx
This was selected as the best answer
Himanshu ParasharHimanshu Parashar
Hi Hardik,

You can call a single javascript function and from there you can call multiple js function
 
<apex:outputpanel id="jspanel">
<script>
var controllervariable='{!controllervariable}';
function fnoncomplete()
{
   if(controllervariable=='xxx')
      //logic
   else if(controllervariable=='yyy')
    //logic

   return false
}

</script>

</apex:outputpanel>


<apex:actionFunction action="{!testMethod}" name="testFunction" oncomplete="fnoncomplete()" reRender="jspanel,testId"/>

I hope it will help you.

Thanks,
Himanshu
Hardik ChavdaHardik Chavda
Thanks Himanshu and William, both of you. This was really helpful and solved my problem.