Activity › Forums › Salesforce® Discussions › Is it possible to call javascript function in an apex controller?
Tagged: Javascript, Salesforce Apex Controller
-
Is it possible to call javascript function in an apex controller?
Posted by Shubham on April 1, 2016 at 7:58 AMIs it possible to call Javascript function in an apex controller? If yes can somebody explain it with a simple example.
shariq replied 7 years, 7 months ago 5 Members · 4 Replies -
4 Replies
- [adinserter block='9']
-
Hi,
Try it
<apex:page controller=”calljavascript_cls” >
<script>
function func()
{
alert(‘function calling’);
}
</script>
<apex:outputText value=”{!callfunc}” escape=”false”></apex:outputText>
</apex:page>
——– apex class ————–
public class calljavascript_cls
{
public string callfunc{get;set;}
public calljavascript_cls()
{
callfunc='<script> func(); </script>’;
}
}
Hope this helps.
Thanks.
-
Hi,
I found this online, try this and let me know-
Controller –
public class showAlertRandom {
public boolean showAlert { get; set; }
public Decimal numberValue { get; set; }
public showAlertRandom() {
doRandomAlert();
}
public void doRandomAlert() {
numberValue = Math.random();
showAlert = numberValue < 0.5;
}
}VF page –
<apex:page controller=”showAlertRandom” showHeader=”true”>
<apex:form id=”form”>
<apex:outputText rendered=”{!showAlert}” id=”alert”>
<script>
alert(‘Hello World’);
</script>
</apex:outputText>
<apex:outputText rendered=”{!not showAlert}” id=”noAlert”>
No alert this time, sorry. Random value was: {!numberValue}.
</apex:outputText>
<apex:actionPoller interval=”5″ action=”{!doRandomAlert}” reRender=”form” />
</apex:form>
</apex:page>Hope this helps.
Log In to reply.