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
deepak kumar 13deepak kumar 13 

Navigate from VF page to google search

hi,
I have a VF Page it contains search input search box and search button.If i entered any value in search text box like "Car" it should redirect from VF page to the google search page with keyword name and displays results regarding cars. 

Is it possible using VF page.
Please suggest me how to approach...

Thank you.
 
Best Answer chosen by deepak kumar 13
ManojjenaManojjena
Hi Deepak,

Either you can use action method like below .
public class Googlequery{
  public string qry{get;set;}
  
  public pagereference searchpage(){
    Pagereference pg=new pagereference('http://www.google.com/search?q='+qry);
    pg.setRedirect(true);
    return pg;
  }
}
////////////////////
<apex:page controller="Googlequery">
 
  <apex:pagemessages />
  <apex:form >
  
   <apex:pageBlock >
     <apex:outputLabel >Query Google</apex:outputLabel>
     <apex:inputText value="{!qry}"/>
    </apex:pageBlock>
   <apex:commandButton value="Search keyword" action="{!searchpage}"/> 
  </apex:form>
</apex:page>

Or you have add reRender in action function component to call the method .

Thnaks 
Manoj

All Answers

bob_buzzardbob_buzzard
You can redirect to google and pass the query string on the URL, e.g.:

    https://www.google.co.uk/search?q=visualforce

So you can either build a pagereference server side and redirect, or change the window.location via JavaScript.
ManojjenaManojjena
HI Deepak,

You can get the search text value in your controller by assigning a getter setter ,assume 
public string searchText{get;set;}

Once you will click on the button  it should call  method and that method shound have return type PageRefrenece .

Finally in page Reference you need to pass the URl like below 
https://www.google.co.in/search?newwindow=1&site=&source=hp&q=+searchText

If you need any help on pageReference Please check below link .

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_system_pagereference.htm

Thnaks 
Manoj

 
deepak kumar 13deepak kumar 13

Thanks for your reply bob & Manoj,

Below is my code 
VF Page:
<apex:page controller="Googlequery">
  <script type="text/javascript">
     function jsfind()
    {
       callgoogle();   
    }
  </script>
  <apex:pagemessages/>
  <apex:form>
  <apex:actionFunction name="callgoogle" action="{!searchpage}"/>
   <apex:pageBlock>
     <apex:outputLabel>Query Google</apex:outputLabel>
     <apex:inputText value="{!qry}"/>
    </apex:pageBlock>
   <apex:commandButton value="Search keyword" onclick="jsfind()"/> 
  </apex:form>
</apex:page>


Apex Class :
public class Googlequery{
  public string qry{get;set;}
  
  public pagereference searchpage(){
    Pagereference pg=new pagereference('http://www.google.com/search?q='+qry);
    pg.setRedirect(true);
    return pg;
  }
}

i tried using javascript but when i click on Search Keyword button i dont get changes in my visualforce page.
How to resolve using javascript.
Thanks in advance.

ManojjenaManojjena
Hi Deepak,

Either you can use action method like below .
public class Googlequery{
  public string qry{get;set;}
  
  public pagereference searchpage(){
    Pagereference pg=new pagereference('http://www.google.com/search?q='+qry);
    pg.setRedirect(true);
    return pg;
  }
}
////////////////////
<apex:page controller="Googlequery">
 
  <apex:pagemessages />
  <apex:form >
  
   <apex:pageBlock >
     <apex:outputLabel >Query Google</apex:outputLabel>
     <apex:inputText value="{!qry}"/>
    </apex:pageBlock>
   <apex:commandButton value="Search keyword" action="{!searchpage}"/> 
  </apex:form>
</apex:page>

Or you have add reRender in action function component to call the method .

Thnaks 
Manoj
This was selected as the best answer
deepak kumar 13deepak kumar 13
Hi manoj.

The above code does not redirect into google search page.Can you explain what mistake i did there??>

thanks,
deepak 
Dhriti MoulickDhriti Moulick
Hi Deepak,

    The above code is absolutely correct.Can you please explain me what error you are exactly facing?
deepak kumar 13deepak kumar 13
Hi Dhriti,

When i search any keyword like "Car" in input text box.It simply loads with empty page in salesforce.How can i approach to redirect into google search page..

Please help me,
Thanks,
Deepak
ManojjenaManojjena
Hi Deepak ,

Have you tested the above code ,or have you added the reRender in your action Function . I have tested in my dev org it is working fine .
Please let me the error you are getting .
bob_buzzardbob_buzzard
This is because you are trying to iframe in google, but their X-Frame-Options header disallows this.  This implies that your VF page is embedded inside another Salesforce page.  You'll need to make your VF page top level for this to work I'm afraid. 
deepak kumar 13deepak kumar 13
Hi,

I removed reRender and its working fine. And i have one more modification is that i need to open that google search page in a new window.

Thanks,
Deepak
Dhriti MoulickDhriti Moulick
Dhriti Moulick
Hi Deepak,

    Please check the following code:

 <apex:page controller="Googlequery" id="pg">
    <script>
        function redirectToGoogle(){
           var a=document.getElementById('pg:frm:pg:input').value;
           window.parent.open('http://www.google.com/search?q='+a);
        }
    </script>
  
    <apex:form id="frm">
        <apex:pageBlock id="pg">
             <apex:outputLabel id="op">Query Google</apex:outputLabel>
             <apex:inputText value="{!qry}" id="input"/>
        </apex:pageBlock>
        <apex:commandButton value="Search keyword" onclick="redirectToGoogle();" id="cmd"/> 
    </apex:form>
</apex:page>


This code will make you open google search in new window.
deepak kumar 13deepak kumar 13
Thanks 
Dhriti Moulick..Your Code Really worked..Thanks to everyone..
Michaela ChrysostomouMichaela Chrysostomou
Thank you for this is amazing.
Manojj can you provide me a test class in order to pass the controller from Sandbox to Production? 

Here is my Controller
public class Googlequery{
 public string qry{get;set;}

      public pagereference searchpage(){
    Pagereference pg=new pagereference('https://docs.google.com'+qry);
  pg.setRedirect(true);
   return pg;

      }
      
      public pagereference searchpage1(){
    Pagereference pg=new pagereference('https://docs.google.com/spreadsheets'+qry);
  pg.setRedirect(true);
   return pg;

      }
      
    }

------------------------------------
VF Page

<apex:page controller="Googlequery">
<apex:pagemessages />
<apex:form >
<apex:pageBlock >
<apex:outputLabel >Google Docs</apex:outputLabel>
</apex:pageBlock>
<apex:commandButton value="Cyprus Live" action="{!searchpage}"/>
<apex:commandButton value="UK Live" action="{!searchpage1}"/> </apex:form>
</apex:page>