A
A
Airat Shakurov2016-09-17 12:20:15
Bootstrap
Airat Shakurov, 2016-09-17 12:20:15

How to close bootstrap modal window when clicking back button in browser?

On the site 2test.shakurow.ru there is a section "natural stones" and so there, when you click on the details and prices, a modal window opens, but after opening, if you press the back button (browser back button), then we will go to the previous site, in general, the essence is such that The modal window closed as if we pressed the Esc button
Help please) Maybe someone has already solved such issues?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Airat Shakurov, 2016-09-18
@Shakurow

Solution
This is how I solved my question:

$('.modal').on('shown.bs.modal', function (e) {
  window.location.href = '#' + $(this).attr('data-m');
});

$('.modal').on('hidden.bs.modal', function (e) {
  history.pushState('', document.title, window.location.pathname);
});

$(window).bind('hashchange', function() {
  if(window.location.hash == ''){
    $('.modal').modal('hide');
  }
});

I
iBird Rose, 2016-09-17
@iiiBird

as an option when opening to anchor. type #test
then when you click back it will go back to the main

E
Edward, 2016-09-17
@edvardpotter

$('.modal').on('hidden.bs.modal', function (e) {
  history.pushState('', document.title, window.location.pathname);
  console.log($(this).attr('data-m'));
});

$(window).bind('hashchange', function() {
  if(window.location.hash == ''){//Если после # ничего нет, значит закрываем модальное окно(попробуйте открыть модальное окно и в адресной строке удалить все после # после чего у вас закроется модальное окно)
    $('.modal').modal('hide');
  }
});

Taken from your example .
Those. you just add the page supposedly the previous one and there you already process the action you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question