Reputation: 1147
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?
Upvotes: 19
Views: 73448
Reputation: 179
On bootstrap 5 and don't use Jquery here my answer;
First we assign an id to the button, select to button and then when page load click automatically this button.
document.getElementById("btn-my").click();
and in css;
#btn-my {
display: none;
}
That's it.
Upvotes: 0
Reputation: 1073
On Bootstrap 5, just create a modal variable with options and runs it with .show()
var myModal = new bootstrap.Modal(document.getElementById('modal'), {keyboard: false, backdrop: 'static'});
myModal.show();
Without Jquery, you cannot use this;
myModal.modal('show');
Upvotes: 1
Reputation: 3897
On Bootstrap 4 --
Add the show class to your modal
<div class="modal fade show" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
and following css --
.modal {
display:block;
}
Upvotes: 0
Reputation: 17
<div id="modal1" class="modal" data-show role="dialog">
data-show help you
Upvotes: -3
Reputation: 6740
Create a CSS class for .modal
and add display:block
. Also give in
class to modal div.
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.
Upvotes: 24
Reputation: 49
You could utilize the page onload function
<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>
<body onLoad="$('#my-modal').modal('show');">
<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>
</body>
Upvotes: 0
Reputation: 39
I spent some time trying to accommodate this and found that this worked fine for my needs which incorporated a single login page... Obviously this is not the finished article but an excellent base from which I was able to start with. Hope it is of some use to someone...
<body style="background: #555;">
<!-- Modal -->
<div class="modal-dialog" style="margin-top: 20%;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</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><!-- /.modal-content -->
</div>
</body>
Upvotes: 1