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
Jina ChetiaJina Chetia 

How to make "javascript:history.back()" work in VisualForce

Hi,

I have to create a back button which will do a browser back operation but somehow the onclick="javascript&colonhistory.back();" is not working for the button. Here is the piece of code below.

Code:
<apex:facet name="footer">
       <apex:commandButton  onclick="javascript&colonhistory.back();" value="Back"/>
</apex:facet>

 Can you anyone please suggest me how to do this?

Regards,
Jina

Ron HessRon Hess
this may work for you
Code:
<apex:page >
 <apex:form >
<apex:commandButton immediate="true" onclick="javascript:back();" value="Save" id="theButton"/></apex:form>
</apex:page>

 
, it did for me in Firefox, but not safari.


Jina ChetiaJina Chetia
It is not working for me even in firefox. Is there any other way to achieve this functionality. I am attaching the full code of my page. I am reloading the same page with different questions depending on the previously answered question and the 'Back' button should take me to the previous question
Code:
<apex:page controller="Wizard_Step1_Controller">
        <apex:form>            
                    <apex:pageBlock title="Section 1" rendered="{!showFirstSection}">
                            Is this a Pre-Printed Contract— <br/><p/>
                            <apex:commandButton action="{!showSection4}" value="Yes" styleClass="btn"></apex:commandButton>
                            <apex:commandButton action="{!showSection2}" value="No" styleClass="btn"></apex:commandButton>
                    </apex:pageBlock>
                    <apex:pageBlock id="Section2" title="Section 2" rendered="{!showSecondSection}">
                           Are there additional payments or  is the DR off rate table– <br/><p/>
                            <apex:commandButton action="{!showNextPage}" value="Yes" styleClass="btn"></apex:commandButton>
                            <apex:commandButton action="{!showSection3}" value="No" styleClass="btn" ></apex:commandButton>        
                    </apex:pageBlock>
                    <apex:pageBlock title="Section 3" rendered="{!showThirdSection}">
                           Have you or CGO identified any contractual clauses with a significant difference  to standard T&amp;Cs <br/><p/>
                            <apex:commandButton action="{!moveNextPage}" value="Yes" styleClass="btn"></apex:commandButton>
                            <apex:commandButton action="{!exitDealProtocol}" value="No" styleClass="btn" ></apex:commandButton>  
                    </apex:pageBlock>
                    <apex:pageBlock title="Section 4" rendered="{!showExit}">
                            Deal Protocol is not required  <p/>
                            <apex:commandButton value="Exit" styleClass="btn"></apex:commandButton>
                    </apex:pageBlock>
                    <apex:pageBlock title="Section 4" rendered="{!showFourthSection}">
                            Deal Protocol is required  <p/>
                            <apex:commandButton action="{!newPage}" value="Continue" styleClass="btn"></apex:commandButton>
                    </apex:pageBlock>
                    <apex:commandbutton immediate="true" onclick="javascript&colon;back();" value="Back" id="theButton"/>
        </apex:form>
</apex:page>

public class Wizard_Step1_Controller 
{
     Boolean showExit= false;
     Boolean thirdSection = false;
     Boolean secondSection = false;
     Boolean fourthSection = false;
     Boolean firstSection = true;
     
     public pagereference showSection4() 
     {
        setShowExit(true);
        setShowSecondSection(false);
        setShowThirdSection(false);
        setShowFourthSection(false);
        setShowFirstSection(false);
        return null; 
     }

     public pagereference showSection2()
     {
        setShowFirstSection(false);
        setShowSecondSection(true);
        setShowExit(false);
        setShowThirdSection (false);
        setShowFourthSection(false);
        return null;
     }
    
     public pagereference showSection3() 
     {
        setShowFirstSection(false);
        setShowExit(false);
        setShowSecondSection(false);
        setShowThirdSection(true);
        setShowFourthSection(false);
        return null; 
     }

     public pagereference showNextPage()
     {
        setShowFirstSection(false);
        setShowSecondSection(false);
        setShowExit(false);
        setShowThirdSection(false);
        setShowFourthSection(true);
        return null;
     }
      public pagereference moveNextPage() 
     {
        setShowFirstSection(false);
        setShowSecondSection(false);
        setShowThirdSection(false);
        setShowExit(false);
        setShowFourthSection(true);
        return null; 
     }

     public pagereference exitDealProtocol()
     {
        setShowFirstSection(false);
        setShowSecondSection(false);
        setShowThirdSection(false);
        setShowExit(true);
        setShowFourthSection(false);
        return null;
     }
     public PageReference newPage() 
     {
      PageReference pr = Page.Wizard_Step2;
      pr.setredirect(true);
      return pr;
    }
     public Boolean getShowExit()
     {
        return this.showExit;
     }
     public void setShowExit(boolean showExit)
     {
        this.showExit= showExit;
     }
     public Boolean getShowFirstSection()
     {
        return this.firstSection;
     }
     public void setShowFirstSection(boolean firstSection )
     {
        this.firstSection = firstSection ;
     }
     public Boolean getShowThirdSection()
     {
        return this.thirdSection;
     }
     public void setShowThirdSection(boolean thirdSection )
     {
        this.thirdSection = thirdSection;
     }
     public Boolean getShowSecondSection()
     {
        return this.secondSection;
     }
     public void setShowSecondSection(boolean secondSection )
     {
        this.secondSection  = secondSection;
     }
     public Boolean getShowFourthSection()
     {
        return this.fourthSection;
     }
     public void setShowFourthSection(boolean fourthSection )
     {
        this.fourthSection= fourthSection;
     }  
}

 

Ron HessRon Hess
I think i understand

Your best bet is to create a real action for that button, one that determines which section it is on , then calls the proper section to go to

something like this

pagereference back() {

if   thirdSection == true
  return showSection2();

etc...


}

this will be the most reliable method to build your wizard, and does not depend on javascript Back().
Jina ChetiaJina Chetia
Thanks Ron, I will try doing this and definately it should work. :)
FengFeng
this work for me:
Code:
<apex:facet name="footer">
       <apex:commandButton  onclick="javascript:window.top.location = '{!ReturnURL}';return false;" value="Back"/>
</apex:facet>

 
Jina ChetiaJina Chetia
Thanks Feng but what should this method '{!ReturnURL}' return? I don't have any retURL as a query parameter in my URL. The URL of the current page looks like https://tapp0.salesforce.com/apex/Wizard_Step1?id=001T0000009RuZl where id is the AccountId of the Account Page from where I am calling this page.