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 6 years, 3 months ago 5 Members · 4 Replies -
4 Replies
-
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.
Popular Salesforce Blogs
Top 6 Salesforce Donation Apps to Maximize Nonprofit Fundraising
We’ll be going over the most excellent Salesforce gift apps that can bring your gathering pledges to another level. 1. Brickwork It is the Salesforce…
Tips for Customizing Web-to-Lead Forms in Salesforce
Salesforce Web-to-Lead is the default way to import Leads into Salesforce from a web form. The way it works is simple: you select the fields…
Salesforce Process Automation Best Practices
Salesforce Process Builder was by far one of Salesforce Users' most helpful and popular features. Salesforce uses Process Builder to automate business operations. Relatively new…