Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
After reload browser still the window.beforeunload event is showing the confirmation alert when try to exist the page

In my application there are some create update forms exist. When user giving values into the input field of these form accidentally if user press the back button or reload button of the browser there would be a confirmation message . I have used 'window.beforeunload' event to show the alert. The alert is appearing fine. But there is some problem I am facing. Scenario-1:Sometimes the alert is appearing when there is no change happened in input field and reload the browser or press the back button. Scenario-2:Suppose user entered some value in input field and reload the browser confirmation message appears. User allow to reload the page. After reload the page if user wants to leave the page still the confirmation message is appearing. I can not understand why the message is appearing after allow the page reload. I am using the following code -

 $(document).ready(function(){
                        //ArrElem is an array which is holding the initial value of each field when the form open 
                         $('#dvModal').on('change paste', 'select,input,textarea', function (e) {
                            if (typeof ArrElem != 'undefined') {
                                for (var i = ArrElem.length; i--;) {
                                    if (ArrElem[i].indexOf($(this).attr('id')) >= 0) {
                                        if ($(this).attr('type') == "checkbox" && ($(this).is(":checked").toString() != ArrElem[i].split(',')[1]))
                                            {
                                            ischanged = true;
                                        }
                                        else if ($(this).attr('type') != "checkbox" && $(this).val() != ArrElem[i].split(',')[1]) {
                                            ischanged = true;
                                        }
                                        else
                                            ischanged = false;
                    
                    
                                        break;
                                    }
                                }
                            } else {
                                if ($(this).val() != $(this).data('val')) {
                                    ischanged = true;
                                }
                                else
                                    ischanged = false;
                            }
                        }); 
                    });
                    /// Using this code to display the alert when click on back,reload button of the browser 
            
            window.addEventListener("beforeunload", (e) => {            
                       if (ischanged) {
                          e.returnValue = null;
                          ischanged = false;
                          return null;
                            }
                        });

Probably I am doing some mistake. If so please suggest me what wrong logic I am using here. If there are any other approach exists please suggest me. I need to implement this very common feature in my application but I am struggling.

Thanks in advance.

Soumen

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda