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
SimrinSimrin 

Calling javascipt from Controller

 Is there way where i can call the javascript from controller ?  I want to execute a javascript when the controler function ends
Best Answer chosen by Simrin
Bhanu MaheshBhanu Mahesh
Hi Simrin, you can make use of onComplete attribute available on commandButton, actionSupport,actionFunction, pageBlockButton to call the java script method after completion of the controller method specified in action attribute

Refer the below code snippet
<apex:page Controller="XXXXX">
	<apex:form>
		<apex:pageBlock>
			<apex:commandButton values="Submit" action="{!controllerMethod}" onComplete="javaScriptMethod();" />
		</apex:pageBlock>
	<apex:form>
	<script>
		function javaScriptMethod(){
		//logic
		}
	</script>
</apex:page>

In the above code snippet, java script method "javaScriptMethod" will be called after completeion of "controllerMethod" specified in action attribute

Regards,
Bhanu Mahesh
 

All Answers

Bhanu MaheshBhanu Mahesh
Hi Simrin, you can make use of onComplete attribute available on commandButton, actionSupport,actionFunction, pageBlockButton to call the java script method after completion of the controller method specified in action attribute

Refer the below code snippet
<apex:page Controller="XXXXX">
	<apex:form>
		<apex:pageBlock>
			<apex:commandButton values="Submit" action="{!controllerMethod}" onComplete="javaScriptMethod();" />
		</apex:pageBlock>
	<apex:form>
	<script>
		function javaScriptMethod(){
		//logic
		}
	</script>
</apex:page>

In the above code snippet, java script method "javaScriptMethod" will be called after completeion of "controllerMethod" specified in action attribute

Regards,
Bhanu Mahesh
 
This was selected as the best answer
SimrinSimrin
<apex:inputText id="id1"  onkeyup="Search('{!$Component.id1}');" value="{!test}">
                </apex:inputText>


I have something like this, Want to call two javascript, 
Search('{!$Component.id1}'),and clear(); but cleart should be executed after the Search('{!$Component.id1}'),

Search calls the action fucntion,which in turn call the Controller function.

I ttied to execute, Serch and Clear, one after other but they are both called before the ewxectuion of backend code

Bhanu MaheshBhanu Mahesh
Then add onComplete="clear();" in ActionFunction like
<apex:actionFunction name="action functionName" action="ControllerMethod" onComplete="clear();" />

Regards,
Bhanu Mahesh
SimrinSimrin
I used setTimeout, in javascript, it worked well ifor my usecase