0

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.

Masked Man
  • 2,176
  • 2
  • 22
  • 41

1 Answers1

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