Activity Forums Salesforce® Discussions Is it possible to call javascript function in an apex controller?

  • Satya

    Member
    April 1, 2016 at 8:19 am

    Try It:-
    https://developer.salesforce.com/forums/?id=906F000000093zeIAA

    • This reply was modified 8 years, 7 months ago by  Satya.
    • This reply was modified 8 years, 7 months ago by  Satya.
  • Parul

    Member
    September 19, 2018 at 9:08 pm

    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.

  • shariq

    Member
    September 19, 2018 at 10:38 pm

    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