Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 439
Sai Kiran Sripada Avatar asked Dec 27 '13 03:12

Sai Kiran Sripada


People also ask

How do you trigger a modal 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.

Do you need jQuery for Bootstrap modal?

If you are using Bootstrap (js), jQuery is required.


1 Answers

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.

like image 187
Surjith S M Avatar answered Oct 03 '22 05:10

Surjith S M