Home > other >  Local fallback for jquery.js, bootstrap.css and bootstrap.js incase CDNs are down or locall internet
Local fallback for jquery.js, bootstrap.css and bootstrap.js incase CDNs are down or locall internet

Time:09-19

I need to provide a local fallback if the CDNs are down for jquery.js, bootstrap.css and bootstrap.js.

With jQuery I can do

<script src="https://code.jquery.com/jquery-3.6.1.min.js"  integrity="" crossorigin=""></script>
<script>window.jQuery || document.write('<script src="scripts/libraries/jquery-3.6.1.min.js"><\/script>')</script>

With bootstrap.css I can do using onerror

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" one rror="this.onerror=null;this.href='frameworks/bootstrap.min.css';" integrity="" crossorigin="" />

But when it comes to bootstrap.js the following does not work while I am using the same code as the above jQuery

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="" crossorigin=""></script>
<script>window.jQuery || document.write('<script src="frameworks/bootstrap.bundle.min.js"><\/script>')</script>

CodePudding user response:

It seems you already have the answer for this but encase it helps:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
    if (!window.bootstrap) { 
        // if the bootstrap object is not present
        let bootstrapScript = document.createElement("script");
        bootstrapScript.setAttribute("src", "frameworks/bootstrap.bundle.min.js");
        document.getElementsByTagName("head")[0].appendChild(bootstrapScript);
    }
</script>

<link rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" 
  one rror="this.onerror=null;this.href='frameworks/bootstrap.min.css';this.removeAttribute('integrity');this.removeAttribute('crossorigin');"
  integrity="" 
  crossorigin="" />

I hope this is useful still

  • Related