I wonder if there's a way to tell browser load a resource like a JavaScript script from server A (e.g. a CDN which has many benefits) and if it was not successful load it from server B (which can be my own server or so) as a fallback solution.
Asked
Active
Viewed 211 times
0
-
Yes that will do, but I thought there would be a better way built into the `HTML` itself. – Masked Man Mar 07 '20 at 15:12
1 Answers
1
Most of the JavaScript used in browsers has objects that appear globally.
It detects the presence or absence, and if not, writes <script>
with document.write
.
console.log($.fn.jquery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.5/jquery.notfound.js"></script>
<script>window.jQuery || document.write(`<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"><\/script>`)</script>
If async is applied, the timing of loading will be "somewhere before onload". So, it is a flow of setting an event on window.onload
and generating a element in the DOM if it is not loaded.

sanriot
- 804
- 4
- 13
-
That will do, but I thought there would be a better way built into the HTML itself. but thanks anyway :) – Masked Man Mar 07 '20 at 15:13
-
@MaskedMan As far as read the HTML Standard, it is not to be in the standard. – sanriot Mar 07 '20 at 15:24