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
TapasviniTapasvini 

pagereference using javascript

I have a javascript function which is called on button click. In this function I want to call apex controller method and pass the value that I have in java script .The apex controller method which is called by javascript returns a pagereference based on the value passed through javascript.. So, how can I code for this?

Rahul SharmaRahul Sharma

Tapasvini, You need to check apex:actionFunction

MagulanDuraipandianMagulanDuraipandian

Hi Tapasvini,

Sample code:

VF:

<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}" />
    
    <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>

 

Controller:

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;
    }       
}

 

If this is the solution, kindly mark it as solution

 

For more info visit www.infallibletechie.blogspot.com