1

I have a simple modal that I took from Bootstrap example code. It's like this:

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <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>

I want to be able to close the modal with the back button on a phone, because it takes up the whole page on a mobile device, and searching for the "x" at the top right isn't very convenient. I took a look at the documentation, but there is only an option to close the modal with the Escape key on a keyboard, and no such solution for a mobile device. Can anyone assist me?

Mihail Ivanchev
  • 403
  • 6
  • 22
  • I am not aware of mobile development, But if you can catch hold of the back button pressed event then you can trigger the modal close event. – Rajshekar Reddy Apr 12 '16 at 08:00
  • This is a website. I just need this to work on a mobile back button. It has the same function as pressing the back button on a page. I just don't know how to catch said action, or exploit it. – Mihail Ivanchev Apr 12 '16 at 08:07

2 Answers2

2

I made it work for my case implementing both https://stackoverflow.com/a/42114285/5118339 and https://stackoverflow.com/a/25464754/5118339, and some fixes

function handleBackPress(event) {
    event.preventDefault();
    event.stopPropagation();

    $('.modal').modal('hide');
    $('.modal-backdrop').remove();
}

var closedModalHashStateId = "#modalClosed";
var openModalHashStateId = "#modalOpen";

/* Updating the hash state creates a new entry
 * in the web browser's history. The latest entry in the web browser's
 * history is "modal.html#modalClosed". */
window.location.hash = closedModalHashStateId;

$(window).on('popstate', this.handleBackPress);
document.addEventListener("backbutton", this.handleBackPress, false);

/* The latest entry in the web browser's history is now "modal.html#modalOpen".
 * The entry before this is "modal.html#modalClosed". */
$('.modal').on('show.bs.modal', function(e) {
  window.history.pushState('forward', null, './'+openModalHashStateId);
});  

/* When the user closes the modal using the Twitter Bootstrap UI, 
 * we just return to the previous entry in the web 
 * browser's history, which is "modal.html#modalClosed". This is the same thing
 * that happens when the user clicks the web browser's back button. */
$('.modal').on('hide.bs.modal', function(e) {
  window.history.back();
});  
saccodd
  • 972
  • 9
  • 23
  • If I have multiple modals open then this closes all of them. Please make changes to close it one by one. The most recent one first, then the next one open on screen and so on. I have raised a question for that here https://stackoverflow.com/questions/69260548/close-bootstrap-modals-using-phones-back-button-one-by-one – Relaxing Music Sep 20 '21 at 21:13
1

Although i would recommend closing the modal with a simple X or close button, you should be able to listen to event via:

$(window).on("navigate", function (event, data) {
  var direction = data.state.direction;
  if (direction == 'back') {
    // do something
  }
  if (direction == 'forward') {
    // do something else
  }
});

You need to listen to navigation event and state.direction.

Have you thought about keeping the X in a static header on the modal? And the content is scrollable..

Oam Psy
  • 8,555
  • 32
  • 93
  • 157
  • That would be easier to do, I have thought about it, but the person I am making this for, wants to be able to close the modal with the back button on his phone – Mihail Ivanchev Apr 12 '16 at 08:17
  • @MihailIvanchev - fair enough, please try the above. Dont forget to upvote/mark as answer if helpful. – Oam Psy Apr 12 '16 at 08:19
  • I have done the following: `` Does not work I've also tried to hook the modal via id, but it still does not work (eg: #myModalHorizontal instead of .modal) – Mihail Ivanchev Apr 12 '16 at 08:24