Skip to content
This repository was archived by the owner on Feb 17, 2023. It is now read-only.

Commit 27bf94e

Browse files
committed
Init
0 parents  commit 27bf94e

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 LouisSung
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LoadBootstrap.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<!-- Bootstrap core CSS -->
4+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
5+
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
6+
</head>
7+
8+
<body>
9+
<!-- Bootstrap core JavaScript -->
10+
<!-- jQuery first, then Popper.js, then Bootstrap JS
11+
Put at the end in order to speed up loading. -->
12+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
13+
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
14+
crossorigin="anonymous"></script>
15+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
16+
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
17+
crossorigin="anonymous"></script>
18+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
19+
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
20+
crossorigin="anonymous"></script>
21+
22+
<script>
23+
// MODIFY SRC TO YOUR OWN PATH
24+
// Test if jQuery is loaded. If not, load it locally.
25+
window.jQuery || console.log("Local jQuery") || document.write('<script type="text/javascript" src="/static/js/jquery-3.3.1.slim.min.js"><\/script>');
26+
// First, make sure jQuery is loaded.
27+
function defer(method, i = 0) { if (window.jQuery) { method(); } else if (i < 999) { setTimeout(function () { defer(method, ++i) }, 50); } }
28+
function externalJS(url, log) { let script = document.createElement("script"); script.type = 'text/javascript'; script.src = url; $("body")[0].appendChild(script); if (log) { console.log(log); } }
29+
defer(function () {
30+
// Second, test Bootstrap.css by its body color. Should be "rgb(33, 37, 41)" (#212529) if loaded. (For Bootstrap 4.0.0~4.3.1)
31+
$("body").css("color") === "rgb(33, 37, 41)" || console.log("Local Bootstrap CSS") || $("head").append('<link rel="stylesheet" href="/static/css/bootstrap.min.css">');
32+
// Last, test if Proper.js, Bootstrap.js and Bootstrap.css are loaded.
33+
window.Popper || externalJS("/static/js/popper.min.js", "Local Popper");
34+
window.jQuery.fn.modal || externalJS("/static/js/bootstrap.min.js", "Local Bootstrap JS");
35+
});
36+
</script>
37+
</body>
38+
</html>

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Fallback Plan for Loading CSS and JS of Bootstrap 4
2+
## Shows how to load Bootstrap from local if that from remote failed
3+
### For Bootstrap 4.0.0 ~ 4.3.1(at least)
4+
### Some discussions
5+
1. [Stack Overflow](https://stackoverflow.com/questions/25748112/load-local-bootstrap-css-and-js-if-load-fail-from-remote): load local bootstrap css and js if load fail from remote
6+
2. [Edd Mann](https://eddmann.com/posts/providing-local-js-and-css-resources-for-cdn-fallbacks/): Providing Local JS and CSS Resources for CDN Fallbacks
7+
3. [freeCodeCapm](https://www.freecodecamp.org/forum/t/using-a-fallback-code-in-case-bootstraps-cdn-is-down/160753): Using a fallback code in case bootstrap’s cdn is down
8+
### Discussions for this project
9+
By trial and error, I finally found out a better way to check and reload files when failed to load from remote.
10+
1. It use [defer](https://stackoverflow.com/a/17914854) function to make sure jQuery is loaded.
11+
2. It'll try only `999 times` on loading jQuery, so that you won't waste resource on keep trying. (modify as needed)
12+
3. You cannot use `document.write()` as what jQuery does to Poper.js and Bootstrap.js since it'll [delete](https://www.w3schools.com/jsref/met_doc_write.asp) all existing HTML.
13+
4. Also, you cannot use `$("body").append()` as what bootstrap css does, since it [won't works](https://stackoverflow.com/questions/610995/cant-append-script-element)
14+
5. And the `<script></script>` tag should be in the DOM for debugging, so use [`appendChild()`](https://stackoverflow.com/questions/610995/cant-append-script-element#comment17105918_611016) instead.
15+
6. Sample HTML code is avalible in [repository](/LoadBootstrap.html), while the key part (JS code) is shown below.
16+
```html
17+
<script>
18+
// MODIFY ALL URLS FOR SRC TO YOUR OWN PATH ON LOCALHOST
19+
// Test if jQuery is loaded. If not, load it locally.
20+
window.jQuery ||
21+
console.log("Local jQuery") ||
22+
document.write('<script type="text/javascript" src="/static/js/jquery-3.3.1.slim.min.js"><\/script>');
23+
24+
// First, make sure jQuery is loaded.
25+
function defer(method, i = 0) { // defer method 50ms until jQuery is ready or counter exceed 999
26+
if (window.jQuery) { method(); }
27+
else if (i < 999) { setTimeout(function () { defer(method, ++i) }, 50); }
28+
}
29+
function externalJS(url, log) {
30+
let script = document.createElement("script"); script.type = 'text/javascript'; script.src = url;
31+
$("body")[0].appendChild(script); // use appendChild() rather than append()
32+
if (log) { console.log(log); }
33+
}
34+
defer(function () {
35+
// Second, test Bootstrap.css by its body color.
36+
// Should be "rgb(33, 37, 41)" (#212529) if loaded. (For Bootstrap 4.0.0 ~ 4.3.1 (at least))
37+
$("body").css("color") === "rgb(33, 37, 41)" ||
38+
console.log("Local Bootstrap CSS") ||
39+
$("head").append('<link rel="stylesheet" href="/static/css/bootstrap.min.css">');
40+
41+
// Last, test if Proper.js, Bootstrap.js and Bootstrap.css are loaded.
42+
window.Popper || externalJS("/static/js/popper.min.js", "Local Popper");
43+
window.jQuery.fn.modal || externalJS("/static/js/bootstrap.min.js", "Local Bootstrap JS");
44+
});
45+
</script>
46+
```
47+

0 commit comments

Comments
 (0)