While working on a xmlhttprequest in JavaScript, I incorporated a bootstrap spinner within a modal to indicate loading. The modal toggles correctly, but I am struggling to hide it once the loading is complete.
I prefer using vanilla JavaScript for this task due to my familiarity with it, although I also have no issue switching to jQuery if necessary :]
For privacy reasons, I'm utilizing google.com as an example.
var once = 0;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var modal = new bootstrap.Modal(document.getElementById("loading"), {});
modal.hide();
}
else {
if (once == 0) {
var modal = new bootstrap.Modal(document.getElementById("loading"), {});
modal.show();
once++;
}
}
};
xhttp.open("GET", "google.com", true);
xhttp.send();
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap 5 Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12707d7d66616660736252273c203c22">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9cbc6c6dddfdddcdadacbc88dbdbdeabeeebabdbeedfdbffbbba898bdc879cecfc">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="modal" style="background: none !important;" id="loading" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
<div class="modal-dialog modal-dialog-centered" style="background: none !important;">
<div class="modal-content" style="background: none !important; border: none;">
<div class="modal-body" style="background: none !important; text-align: center;">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
If anyone could provide assistance, it would be highly appreciated. Thank you!