简体   繁体   中英

Loading Bootstrap modal after delay not working

I'm working on a cratejoy site, and my client needs a modal to popup after a set amount of time. I added the modal and when I use this it works fine:

$('#myModal').modal('show');

But when I try to use either of these answers from this page , it get a Uncaught TypeError: $(...).modal is not a function error and the modal does not popup. You can see it not working here .

You have multiple versions of jQuery being loaded

Apart from the obvious version with it's own script tag your main.js file also includes it.

Since it loads after bootstrap.js it overwrites the whole jQuery object and removes reference to bootstrap in original version. That is why you get the error shown

Choose which version of jQuery you want to use and remove the other.

Make sure boostrap.js loads after the selected choice

First of all you need to have link to bootstrap.js in your page and loaded after jquery.

Second you have to place the script in a script tag before the end body tag

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

In order to delay the modal you can do this:

setTimeout(function(){
    $('#myModal').modal('show');
}, 5000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM