Calling Controller method using Javascript in Visualforce page

Calling Controller method using Javascript in Visualforce page

Sample Code:


Visualforce page:

<apex:page controller=”sample”>
    <!– Calling Controller Method from Visualforce page –>
   
    <!– Javascript –>
    <script type=”text/javascript”>
        function jsfind()
        {
            callfind();
        }
    </script>
   
    <apex:pagemessages />
   
    <apex:form >
   
    <apex:actionFunction name=”callfind” action=”{!searchAndfind}” reRender=”a” />
   
    <apex:pageBlock >
        <apex:pageblockSection >
            <apex:outputLabel >Query:</apex:outputLabel>
            <apex:inputText value=”{!qry}”/>       
        </apex:pageblockSection>
       
        <apex:pageBlockButtons >
            <apex:commandButton value=”Query” onclick=”jsfind()”/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
   
    </apex:form>

</apex:page>

Apex Class: 

public class sample {

    public String qry{get;set;}

    public pageReference searchAndfind() {
        pageReference pg = new pageReference(‘http://www.google.com/search?q=’+qry);
        pg.setRedirect(true);
        return pg;
    }      
}

Leave a Reply