I'm trying to launch a Bootstrap modal on page load without firing a HTML button or using jQuery.
Here's my code:
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<div id="my-modal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">
Hello World!
</div>
</div>
</div>
</div>
This works when you call
$('#my-modal').modal('show');
But I don't want to call a method or fire a button. Is there a way to launch modal automatically 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.
If you are using Bootstrap (js), jQuery is required.
Create a CSS class for .modal
and add display:block
. Also give in
class to modal div.
Working Demo
CSS
.modal {
display:block;
}
HTML
<div id="my-modal" class="modal fade in">
Edit: Somehow default closing function will not work, you will need to add custom script for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With