Good day,
I'm designing a website that basically works on 1 single page, all information is displayed by pressing buttons that open a modal window, and closed by pressing a button hide
( dismiss ).
The page is designed for mobile use only . Therefore, it is very common for users to try to close the modal with the back/return button on their phones, and doing so exits the site.
Can I implement (maybe with JS) a code to close the modal with this button that mobiles bring?
I have searched for information but I only find it for Android Apps, not for web pages.
.modal-full {
min-width: 100%;
margin: 0;
}
.modal-full .modal-content {
min-height: 100vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#myModal" role="button" class="btn btn-primary" data-toggle="modal">Launch modal</a>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-full" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body p-4" id="result">
<p>The grid inside the modal is responsive too..</p>
<div class="row">
<div class="col-sm-6 col-lg-3">
Content
</div>
<div class="col-sm-6 col-lg-3">
Content
</div>
<div class="col-sm-6 col-lg-3">
Content
</div>
<div class="col-sm-6 col-lg-3">
Content
</div>
<div class="col-sm-6 col-lg-3">
Content
</div>
<div class="col-sm-6 col-lg-3">
Content
</div>
<div class="col-sm-6 col-lg-3">
Content
</div>
<div class="col-sm-6 col-lg-3">
Content
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
After a long time I found this plugin: https://github.com/keaukraine/bootstrap-fs-modal very light and very easy to configure :
What should you do? Apply the class
modal-fullscreen
(proper to the plugin) in the Bootstrap modals and nothing else , the rest is done by JS.You can see how it works in this demo from mobile devices .
If you do not want to use the styles that this plugin brings, you can remove them, leaving only the JS that I share below functional:
There isn't a way to do it for Android with just JS or jQuery .
It can't be done.
What you can do is control the navigation and when that button is pressed do the function you want, like close the modal window.
You could try something like this (of course you have to adapt it to your code, but that's what I can give you without seeing yours):