4

Trying to create a view in codeignitor for a log in page that permanently displays a Bootstrap Modal. I am able to get the modal pop up window by adding the button, but I do not know how to get the modal to display by default when the page loads.

I copied and pasted the following from the docs but nothing shows on the page, it only shows up in the source.

<div 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">Modal title</h4>
     </div>
     <div class="modal-body">
      <p>One fine body&hellip;</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><!-- /.modal-dialog -->

Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
jojojohn
  • 725
  • 2
  • 10
  • 19

3 Answers3

16

Open the modal by javascript on document ready by $('#myModal').modal('show');

Set backdrop and keyboard to false prevent the modal closing when clicking outside it.

$(function() {
$('#myModal').modal({backdrop:false,keyboard:false});
$('#myModal').modal('show');
});

$('#myModal').modal({backdrop:'static',keyboard:false, show:true});

Or set the backdrop to static, from the docs: "Alternatively, specify static for a backdrop which doesn't close the modal on click.", see also Prevent Bootstrap Modal from disappearing when clicking outside or pressing escape?

Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
14

You can create this by removing fade and add show class to modal.

<div class="modal show">
    <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">Modal title</h4>
            </div>

            <div class="modal-body">
                <p>One fine body&hellip;</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><!-- /.modal-dialog -->
</div><!-- /.modal -->

Check the Demo

faremal
  • 50
  • 2
  • 6
Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
4

To make modal undismissable you can set backdrop static without javascript:

 <div class="modal" data-backdrop="static" ... >

Open onload using:

$('#your_modal').modal('show');
Nowdeen
  • 1,401
  • 14
  • 20
  • Does not work when initializing in Javascript (at least not with Bootstrap 4). You then need to pass it as option in js call – Bluesight Jul 20 '20 at 14:07