Load Bootstrap modal on page load without firing button or using jQuery

Sai Kiran Sripada picture Sai Kiran Sripada · Dec 27, 2013 · Viewed 68.9k times · Source

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">&times;</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?

Demo: Fiddle

Answer

Surjith S M picture Surjith S M · Dec 27, 2013

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.