Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 17, 2023 01:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/87a58ba241a10d89e3802fac56694503 to your computer and use it in GitHub Desktop.
Save code-boxx/87a58ba241a10d89e3802fac56694503 to your computer and use it in GitHub Desktop.
Javascript HTML Show Loading Spinner Until Page Loads

JAVASCRIPT HTML LOADING SPINNER UNTIL PAGE LOAD

https://code-boxx.com/show-loading-spinner-until-page-loads/

IMAGE

ajax-loader

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Page Load Order</title>
<!-- (A1) LOADING SPINNER -->
<link rel="stylesheet" href="1b-load-spinner.css">
<script src="1c-load-spinner.js"></script>
</head>
<body>
<!-- (A2) LOADING SPINNER -->
<div id="loading"></div>
<!-- (B) REST OF THE PAGE -->
<p>YOUR CONTENT HERE</p>
<!-- (C) LOAD JS/CSS LAST -->
<script>
// JUST RUN LOADER() IF NO SCRIPTS TO LOAD
window.addEventListener("DOMContentLoaded", () => loader([
"my.CSS", "my.js", "https://cdnjs.cloudflare.com/ajax/libs/WHATEVER.JS"
]));
</script>
</body>
</html>
#loading {
/* (A1) COVER FULL PAGE */
position: fixed;
top: 0; left: 0; z-index: 999;
width: 100vw; height: 100vh;
/* (A2) SPINNER IMAGE */
background-color: black;
background-image: url("ajax-loader.gif");
background-position: center;
background-repeat: no-repeat;
}
function loader (all) {
// (A) ALL LOADED - REMOVE SPINNER
var total = 0, loaded = 0, s,
ready = () => {
loaded++;
if (loaded>=total) { document.getElementById("loading").remove(); }
};
// (B) CREATE <LINK><SCRIPT>
if (Array.isArray(all)) {
total = all.length;
all.forEach(url => {
// (B1) CSS FILE - <LINK>
if (url.toLowerCase().includes(".css")) {
s = document.createElement("link");
s.rel = "stylesheet";
s.href = url;
s.onload = ready; s.onerror = ready;
}
// (B2) JS FILE - <SCRIPT ASYNC>
else {
s = document.createElement("script");
s.async = true;
s.src = url;
s.onload = ready; s.onerror = ready;
}
document.head.appendChild(s);
});
}
// (C) JUST START IF NO SCRIPTS TO LOAD
else { ready(); }
}
<!DOCTYPE html>
<html>
<head>
<title>AJAX Load</title>
<!-- (A) CSS & SCRIPTS -->
<link rel="stylesheet" href="2b-styles.css">
<script src="2c-loader.js"></script>
<script>
window.onload = () => loader("2d-content.html");
</script>
</head>
<body>
<!-- (B) LOADING SPINNER -->
<div id="loading" class="load"></div>
<!-- (C) HTML PAGE AS USUAL -->
<header>Header</header>
<main id="main"></main>
<footer>Footer</footer>
</body>
</html>
#loading {
/* (A1) COVER FULL PAGE */
position: fixed;
top: 0; left: 0; z-index: 999;
width: 100vw; height: 100vh;
/* (A2) SPINNER IMAGE */
background-color: rgba(0, 0, 0, 0.5);
background-image: url("ajax-loader.gif");
background-position: center;
background-repeat: no-repeat;
/* (A3) HIDDEN BY DEFAULT */
display: none;
}
/* (A4) SHOW LOADING */
#loading.load { display: block; }
/* (X) DUMMY PAGE LAYOUT */
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
min-height: 100vh;
padding: 0; margin: 0;
}
main { flex-grow: 1; }
header, main, footer { padding: 10px; }
header { background: #ffd6d6; }
footer { background: #d6e5ff; }
function loader (url, loaded) {
// (A) GET HTML ELEMENTS
var main = document.getElementById("main"),
loading = document.getElementById("loading");
// (B) LOADING SPINNER
loading.classList.add("load");
// (C) AJAX LOAD PAGE
fetch(url)
.then(res => res.text())
.then(txt => {
main.innerHTML = txt;
if (typeof loaded == "function") { loaded(); }
})
.finally(() => loading.classList.remove("load"));
}
<h1>Hello World</h1>
<p>It works!</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment