Open modal bootstrap automatically when loading page

Asked

Viewed 26,954 times

3

People need that as soon as the page load the modal of the bootstrap open on the screen without having to click the button, the way I am doing ta being necessary to click the button for the modal open, follows the code:

<button type="button" class="btn btn-primary construcao" data-toggle="modal" data-target="#exemplomodal">SITE EM CONSTRUÇÃO</button>



<div class="modal fade" id="exemplomodal" tabindex="-1" role="dialog" aria-
 labelledby="myLargeModalLabel">
<div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="gridSystemModalLabel">teste</h4>
        </div>
        <div class="modal-body">
            teste

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
</div>

<script type="text/javascript">

$(document).ready(function() {
    $('#exemplomodal').modal('show');
})
</script>
  • Apparently you’re doing it the right way, strange it’s not working, try the following: $(window). load(Function()' $('#exemplomodal').modal('show'); }); if it doesn’t work, try a 100ms setTimeOut

  • did not work, keeps opening only at the click of the button, in inspect appears that modal is not a function, this error is common ?

  • So probably your error is in the scripts declaration, I will post as a reply, a moment.

  • Just click on the button when you click on the page and give a . Hide() on the button.

1 answer

9


You are using the correct way to open the bootstrap modal:

$(document).ready(function() {
    $('#exemplomodal').modal('show');
})

How the error in the console is

.modal is not a Function

Then probably your problem is in the statement order of the scripts, declare them in the following order:

<!-- jquery -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- bootstrap -->
<script type="text/javascript" src="js/bootstrap.js"></script>
<!-- chamada da função -->
<script type="text/javascript">
$(window).load(function() {
    $('#exemplomodal').modal('show');
});
</script>

This is because bootstrap depends on jquery, that is, jquery must necessarily be declared before.

  • bootstrap is after jquery... '-' all scripts I’m calling at head.. and jquery comes first.. then bootstrap.. however continues to give this modal error is not a Function: follows..

  • Uncaught Typeerror: $(...). modal is not a Function at home:41 at Handle (jquery-1.3.2.min.js:19) at jquery-1.3.2.min. js:19

  • really is something with jquery

  • Hmm, try using the latest version of jquery, https://code.jquery.com/jquery-3.2.0.min.js

  • Use these two, it will probably work: src="https://code.jquery.com/jquery-1.11.3.min.js" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"

  • it worked!!! really was the jquery version that was old! thank you very much friend!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.