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
Shukla YogeshShukla Yogesh 

Calling controller method from JavaScript and Calling Javascript Function from controller

Can somebody explain what are the scenarios for both and how to implement them:
1. Calling controller method from JavaScript
2. Calling Javascript Function from controller
Rupal KumarRupal Kumar
hi,
This is example to Calling controller method from JavaScript and  alling Javascript Function from controller
<apex:page controller="t">
<script>
    function myJavascriptFunc()
    {
        alert('Entered Javascript') ;
        CallApexMethod() ;
    }
</script>
<apex:form >
<apex:actionFunction name="CallApexMethod" action="{!myActionInController}" onComplete="alert('After apex method') ;"/>
  <apex:pageBlock >
        <apex:pageBlockButtons>
            <apex:commandButton value="Hit Me" onclick="myJavascriptFunc() ;"/>
        </apex:pageBlockButtons>
  </apex:pageBlock>
</apex:form>
</apex:page>
controllerhttp://mirketa.com
public class t
{
    public PageReference myActionInController()
    {
        return null ;
    }
}

Thanks
Rupal kumar
 
Vishal Negandhi 16Vishal Negandhi 16
Hi Yogesh, 
1. Calling controller method from JavaScript
Scenario : when you have some javascript libraries being used on your vf page and you want to invoke some apex method from javascript. 
how to call? Invoke an actionFunction from javascript which in turn will call the apex function.

2. Calling Javascript Function from controller
Scenario : when after performing some logic in apex, you want some action on the page using script. 
how to call? You cannot directly call a javascript function from apex class. What you can instead do is, in the oncomplete of your actionFunction that has invoked the apex method, call a javascript method you would want to execute. 

I have given very high level details assuming you just want to get an idea.
Shukla YogeshShukla Yogesh

Thanks Vishal for the clarification!!

Could you please share any code if you have for both implementation scenarios?

Kumaravel MathivananKumaravel Mathivanan
Hi Rupal ,
It's simple Example. We need some functionality in controller method (i.e myActionInController()).