3

I'm having a problem with Bootstrap + jquery loading from CDN sometimes it doesn't load/get imported from CDN for example :

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

It doesn't get loaded in the site when opened. What I want to do is to add the same file in my server too so if it fails to upload this from google server it would load it from mine. for example:

<link rel="stylesheet" href="#mydomain#/css/bootstrap.min.css">

how can I do the above in a way that it won't load it twice if it successfully loads it from CDN (Is there's a way to do so or better way to achieve this?) thanks in advance

8
  • are you sure it doesnt load? maybe your order of loading things isnt good, but cdn deliver pretty reliably Commented Aug 20, 2018 at 8:04
  • 3
    Possible duplicate of Provide local fallback for CSS from CDN Commented Aug 20, 2018 at 8:08
  • yes, it's not getting loaded for some reason it happens with Jquery and with bootstrap. it's not constant but it happens sometimes which is a concern. Commented Aug 20, 2018 at 8:09
  • 1
    Why you don't always load from your server ? Commented Aug 20, 2018 at 8:10
  • 1
    Well you can check if jQuery is loaded (stackoverflow.com/questions/7341865/…) and if it's not loaded you can get it from your server Commented Aug 20, 2018 at 8:15

1 Answer 1

1

This piece of codewill handle script load fails, and allow you to add a fallback attribute that will be loaded instead...

var scripts = document.querySelectorAll("script[data-fallback]");

[].forEach.call(scripts, function(script) {
  var listener = script.addEventListener("error", function() {
  var newScript = document.createElement("script");
  newScript.setAttribute("src", script.getAttribute("data-fallback"));
    document.querySelector("head").appendChild(newScript);
  });
});
<script
  src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.fail"
  data-fallback="https://something-local.com/jquery.js"></script>

Note: This won't work on this page. It's just the nature of Stack Overflow's snippets. Here's a working jsfiddle demo instead...

http://jsfiddle.net/ArchersFiddle/pbfv6q4x/

If you open the console on that page you'll see the original script load error and then the output showing the fallback script was loaded.

Here's the same thing, but for stylesheets...

var stylesheets = document.querySelectorAll("link[rel=stylesheet][data-fallback]");

[].forEach.call(stylesheets, function(stylesheet) {
    var listener = script.addEventListener("error", function(e) {
    var newStylesheet = document.createElement("link");
    newStylesheet.setAttribute("rel", "stylesheet");
    newStylesheet.setAttribute("href", stylesheet.getAttribute("data-fallback"));
    document.querySelector("head").appendChild(stylesheet);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.