Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to show modal popup before leaving the page

<a href="https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h" id="leave">click here to leave the page</a>


<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Changes made may not be saved.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

$("#leave").click(function() {
  $(window).bind('beforeunload', function() {
    return 'Changes you made may not be Saved';
  });
});

here is the js fiddle am working on

like image 740
vishag v.j Avatar asked Aug 29 '18 04:08

vishag v.j


People also ask

How do I show modal pop in the middle of my screen?

Example 1: First, we will design modal content for the sign-up then by using CSS we will align that modal centered(vertically). Using the vertical-align property, the vertical-align property sets the vertical alignment of an element.

How do I get modal to pop on page load?

Answer: Use the Bootstrap . modal('show') method modal('show') method for launching the modal window automatically when page load without clicking anything. A common example of this technique is loading the modal when user landed on the home page and requesting them to subscribe the website newsletter.

How do you show modal dialog?

The Window. showModalDialog() created and displayed a modal dialog box containing a specified HTML document.

How do you make a modal disappear after clicking?

There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.


1 Answers

You could prevent the leave link's default behavior, display the dialog and use timeout to redirect the page in a few seconds.

Example:

$("#leave").click(function(e) {
  e.preventDefault();
  var link = this.href;
  //code to show dialog
  window.setTimeout(function() {    
    window.location.href = link;
  }, 2000);
});

Also, you need to wrap the script codes in a script html tag.

like image 141
Sang Suantak Avatar answered Oct 19 '22 09:10

Sang Suantak