📅  最后修改于: 2023-12-03 14:59:32.639000             🧑  作者: Mango
When developing a website with dynamic content, it is sometimes necessary to load data from a remote URL and display it in a modal popup on the page. Bootstrap 4 provides the necessary tools to achieve this with ease. In this tutorial, we will discuss how to create a Bootstrap 4 modal popup that loads data from a remote URL using Javascript.
Before we begin, it is assumed that you have a working understanding of HTML, CSS, and Javascript. It is also assumed that Bootstrap 4 is already installed and configured in your project.
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Remote Content</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<p>Loading content...</p>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Open Modal</button>
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var url = "https://www.example.com/remote-content.html";
$.ajax({
type: "GET",
url: url,
beforeSend: function(){
$('.modal-body').html('<p>Loading content...</p>');
},
success: function(data){
$('.modal-body').html(data);
},
error: function(jqXHR, textStatus, errorThrown){
console.log(textStatus, errorThrown);
}
});
});
});
In this tutorial, we have discussed how to create a Bootstrap 4 modal popup that loads content from a remote URL using Javascript. By following these steps, you can easily incorporate dynamic content into your website and provide a better user experience for your visitors.