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
lakshmi.sf9lakshmi.sf9 

Facing issue with confirmation alrt box in VF page

Hi,

I have 3 tabs in tab panel.and each tab is having next and previous buttons.If we click on next button on third tab it has to go for another outputpanel i.e overview.
And each button I need rerender functionalities.In each button if user close the window I need to show some confiramtion alert box..But in my code it is working for only first tab.

Can any one please help me..I need confiramtion alertbox on 3 buttons and overview section.

Here is my code :

VF Page :

<apex:page id="pageId" Controller="aaa">
<apex:includeScript value="{!$Resource.Jquery1}"/>
<apex:includeScript value="{!URLFOR($Resource.Jquery1)}" />
<script>
var j$ = jQuery.noConflict();
var tempDisableBeforeUnload = false;

j$(window).on('beforeunload', function()
{
   if (!tempDisableBeforeUnload)
   {
      return 'Leaving this page will lose your changes. Are you sure?';
   }
   else
   {
      //reset
      tempDisableBeforeUnload = false;
      return null;
   }
});
</script>
    <apex:outputpanel rendered="{!NOT(display)}" >
    <apex:form Id="FormId">
    <apex:pageMessages id="MainPageMsg"/>
        <apex:tabPanel selectedTab="{!currentTab}" switchType="client" immediate="true" id="tabPanel">
            <apex:tab label="Tab1" name="one" disabled="{!currentTab<>'1'}" >
                 <apex:inputField value="{!acc.test__c}"/>
                <apex:commandButton value="Next" action="{!nextTab}" onclick="tempDisableBeforeUnload = true;" reRender="tabPanel"/>
            </apex:tab>
            <apex:tab label="Tab2" name="two" disabled="{!currentTab<>'2'}">
                <apex:inputField value="{!acc.test__c}"/>
                <apex:commandButton value="Next" action="{!nextTab}" onclick="tempDisableBeforeUnload = true;" reRender="tabPanel"/>
                <apex:commandButton value="Previous" action="{!prevTab}" onclick="tempDisableBeforeUnload = true;" reRender="tabPanel"/>    
             </apex:tab>
            <apex:tab label="Tab3" name="three"  id="SecondTab" disabled="{!currentTab<>'3'}" >
                <apex:inputField value="{!acc.test__c}"/>
                <apex:commandButton value="Next" action="{!nextTab}" onclick="tempDisableBeforeUnload = true;" reRender="tabPanel"/>
                <apex:commandButton value="Previous" action="{!prevTab}" onclick="tempDisableBeforeUnload = true;" reRender="tabPanel"/>   
            </apex:tab>
        </apex:tabPanel>
    </apex:form>
    </apex:outputpanel>
    <apex:outputPanel id="viewState"  rendered="{!display}">
    overview
    </apex:outputpanel>
</apex:page>

Class :

public class aaa{
Public Account acc {get;set;}
public String currentTab { get; set; }
public boolean display {get;set;}
    public aaa(){
       display = false;
        acc = new Account();
        currentTab = '1';
    }
    public void nextTab() {
        if(currentTab == '1'){
            currentTab = String.valueOf(Integer.valueOf(currentTab)+1);
        }
        else if(currentTab == '2'){
                currentTab = String.valueOf(Integer.valueOf(currentTab)+1);
        }
   
        else if(currentTab == '3'){
            display = true;
            currentTab = String.valueOf(Integer.valueOf(currentTab)+1);  
        }
    }
    public void prevTab(){
    currentTab = String.valueOf(Integer.valueOf(currentTab)-1);
    }   
}
ShashForceShashForce
Hi,

Since you are setting "tempDisableBeforeUnload = true;", the jQuery function is returning "null" instead of your message, and that is what is appearing on the alert box once any of the buttons are clicked. Please change it to "tempDisableBeforeUnload = false" instead, and you will see your message correctly in the alert box whenever you close or refresh.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank