+ Start a Discussion
kishore64kishore64 

how to stop java script execution while clicking on command button

Hi folks,

I have a requirement like,I have a command button and if any one is clicking on this,we are redirceting to another page but when ever the user is closing the same tab or browser,we are throwing an error message through JavaScript(window.onbeforeunload event) but here the problem is,if am clicking on the command button  this script is executing i.e, this error message is coming and i want to stop script execution on clicking on command button.

Please help me out!!!!!
Shashikant SharmaShashikant Sharma
You could make return true on onlcick event of your command button.

onclick="return true;"

If this does not work then please share your code. It will help answering then. 

Thanks
Shashikant
kishore64kishore64
Hi Shashikant,

As you suggested i added onclick attribute in the command button but it's not working.please find the below code......
Controller:

public with sharing class logincntrlr {

        public Login_Logout_Data__c logindata{get;set;}
        public boolean pgmsg{get;set;}
        public string cuserID{get;set;}
        Public logincntrlr(){
            logindata = new Login_Logout_Data__c ();
         }
    
        list<LoginHistory> lginh = [SELECT UserId,LoginTime,SourceIp from LoginHistory];
        public PageReference continuetologin() {
             List<Login_Logout_Data__c> lldata = New List<Login_Logout_Data__c>();
             lldata = [select id,name,Email__c,Active__c,User_Id__c from Login_Logout_Data__c];
             Map<ID,Login_Logout_Data__c> mapLinLout = New Map<ID,Login_Logout_Data__c>();
                 for(Login_Logout_Data__c ll:lldata){
                      mapLinLout.put(ll.User_Id__c,ll);
                  } 
             if(mapLinLout != Null && mapLinLout.containskey(UserInfo.getUserId()) && mapLinLout.get(UserInfo.getUserId()).Active__c==True){
                 pgmsg= true;
                  ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Here Error messgae.....');
                     ApexPages.addMessage(myMsg);
                     
             }    
             else{  
                 pgmsg=false;
                 logindata .Name = UserInfo.getFirstName() + UserInfo.getLastName();
                 logindata .Email__c= UserInfo.getUserEmail();
                 logindata .ownerId= UserInfo.getUserId();
                 logindata .Active__c= True;
                 for(Integer i=0;i<lginh.size();i++){
                     if(UserInfo.getUserId()==lginh[i].UserId){
                         logindata.Login_Time__c= lginh[i].LoginTime;
                         logindata.Session_IP__c= lginh[i].SourceIp;
                     }
                 }
                insert logindata ;
                }
                if(pgmsg==false){
                  Pagereference pg = New pagereference('/001/o');
                     pg.setRedirect(True);
                     return pg;
                 }
                 else
                 return null;
        }
        public PageReference logoutCheck() {
            cuserID = UserInfo.getUserId();
            List<Login_Logout_Data__c> updatelldata =  New List<Login_Logout_Data__c>();
            updatelldata = [select id,name,Email__c,Active__c,User_Id__c from Login_Logout_Data__c where User_Id__c =: cuserID.substring(0,15) AND Active__c = true];
            if(updatelldata!=null && updatelldata.size()>0){
            updatelldata[0].Logout_Time__c=system.now();
            updatelldata[0].Active__c=false;
            update updatelldata;
            }
            return null;
        }
}

Visualforce Page:

<apex:page controller="logincntrlr" showHeader="false">
  <apex:form id="frm" >
            <apex:actionFunction name="logCheck" action="{!logoutcheck}" reRender="frm"/>
            <script>
                var back = false;
                    back = true; 
               window.onbeforeunload = function (e) {
                        if(back == true){
                            logCheck();
                            return "Are you sure to exit?";
                            }
                         else 
                           return null;   
                    }
              </script>
      <apex:pageMessages >
      </apex:pageMessages>
     <body>
          <center><p><apex:commandButton value="Continue to Login" action="{!continuetologin}"/></p></center>
     </body> 
     </html>
  </apex:form>
</apex:page>
Gaurav_SrivastavaGaurav_Srivastava
Hi,

Please define javascript before apex:form as suggested below.

Visualforce Page:

<apex:page controller="logincntrlr" showHeader="false">
    <script>
        var back = false;
            back = true; 
       window.onbeforeunload = function (e) {
                if(back == true){
                    logCheck();
                    return "Are you sure to exit?";
                    }
                 else 
                   return null;   
            }
      </script>
  <apex:form id="frm" >
            <apex:actionFunction name="logCheck" action="{!logoutcheck}" reRender="frm"/>
      <apex:pageMessages >
      </apex:pageMessages>
     <body>
          <center><p><apex:commandButton value="Continue to Login" action="{!continuetologin}"/></p></center>
     </body> 
     </html>
  </apex:form>
</apex:page>

Thanks,
Gaurav