Answer the question
In order to leave comments, you need to log in
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
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');
}
});
as an option when opening to anchor. type #test
then when you click back it will go back to the main
$('.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');
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question