Prevent from closing modal window

Asked

Viewed 2,120 times

2

I would like to know if it is possible to prevent closing the modal window of Bootstrap when updating the page. As we deal with several types of users, I need this to happen so that I can change the status of the banco de dados. With the code below, I was able to prevent users from closing the window by clicking outside it:

data-backdrop="static"

But I also need to prevent the window from closing by pressing the key F5.

Thank you all!

  • It is not possible to "keep" it open because by pressing F5 a new page is "generated". What you can try to do is save the information that the modal is open to when the page loads once again, automatically display it.

1 answer

1


There is no way to prevent you from closing the browser or updating the page, but you can send the user a confirmation window, for example:

window.onbeforeunload = function() {
    return "Você não salvou a sua tarefa, gostaria mesmo de sair?";
};

The problem is that so the question will run in any situation then you can check if the bootstrap window is visible before launching the confirmation:

This way the confirmation only appears if the modal window is open:

$(window).bind("beforeunload", function() {
   if ($("#myModal").is(":visible")) {
       return "Você não salvou a sua tarefa, gostaria mesmo de sair?";
   }
});

Example of use:

<!DOCTYPE html>
<html>
<head>
    <title>Exemplo simples</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css">
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script type="text/javascript">
    $(window).bind("beforeunload", function() {
       if ($("#myModal").is(":visible")) {
           return "Você não salvou a sua tarefa, gostaria mesmo de sair?";
       }
    });
    </script>
</head>
<body>
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
      Ver janela
    </button>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
</body>
</html>
  • Okay. Forgive me for being ignorant, but I’m a Jquery layman. In this case I put it before the tag body, right? When I put it, it didn’t work.

  • @Jose.Marcos Look at the example I added, see if it helps.

  • Perfect William. Thank you very much for the help. It worked perfectly. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.