+ Start a Discussion
tarun.khandelwal1.3965028573349685E12tarun.khandelwal1.3965028573349685E12 

Abrupt Behavior with Browser back and Ajax Call

Hello All,

I am using command button to perform an ajax call.

Please find the code snippet below
Controller:

public with sharing class TestController {
     public TestController(){
         this.flag=true;
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Loading Constructor : '+String.valueOf(flag)));
     }
     
     public PageReference changeFlagValue(){
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Value Before Change : '+String.valueOf(flag)));
          if(flag){
             flag=false;
         }
         else{
             flag=true;
         }
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Value After Change : '+String.valueOf(flag)));
         return null;
     }
     
     public PageReference displayFlagValue(){
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Current Value : '+String.valueOf(flag)));
         return null;
     }
     
     public Boolean flag{get;set;}
}
VF:
<apex:page showheader="false" sidebar="false" controller="TestController">
<apex:form >
<apex:commandButton action="{!displayFlagValue}" value="Display" rerender="pageMessages" />
<apex:commandButton action="{!changeFlagValue}" value="Change" rerender="pageMessages" />
<a href="http://google.com">Move</a>
</apex:form>
<apex:pageMessages id="pageMessages" />
</apex:page>

Command Button : Changes the value of Flag.

Constructor : Reinitializes the flag value to true.
Steps : 
1. Load the Page.
2. Press Display : Shows true
3. Press Change : Shows false
4. Click on Move : 
5. Press Browser Back Button.
6. Constructor Loaded : Value Reset To True
7. Press Display : 
Ajax call using last value before moving to Google Page, But in controller flag value is changed.

 

Can any please tell me the logic behind this behavior.
Thanks

ShashForceShashForce
Hi,

I tried this code in my developer org. When I move to google and press back, and then click Display, I see:

Current Value : true

which lookslike expected. Can you let me know if your question/issue is different?

Thanks,
Shashank
EnreecoEnreeco
I think this is because when you hit the back button the page is loaded again but the Browser uses the "old" viewstate and when using the "display" you see the previous value...it's like the browser is overwriting the view state that is stored in an input hidden, after the page loads
tarun.khandelwal1.3965028573349685E12tarun.khandelwal1.3965028573349685E12
I checked and it's working in chrome and IE, but not in firefox. Seems like firefox caching the data. I know it's not a salesforce issue. But can anyone tell me how to change the behavior of back button in firefox.