Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Is there a way to load internally downloaded js and css files if CDN isnt working

Say i wanted load internally downloaded css and js files as an alternative to the following links if CDN was not working, how would i do it?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

One way I can think of is to use the onerror handler of the script tag, which will be called if the resource was not well loaded and after this you can load dynamically/manually the script from somewhere else.

setTimeout(() => {
  if (window.jQuery) {
    console.log('jQuery is loaded from other source than CDN!');
  }
}, 1000);
// Make sure you do a better way of assuring that your script is being loaded...
<script> 
function onCDNFailed() {
 console.log('CDN failed!');
 
 // Loads from somewhere else..
 const script = document.createElement('script');
 script.onload = () => { console.log('script loaded!'); };
 script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js';
 document.head.append(script);
}
</script>


<script src="https://ajax.googleapis.com/123.js" onerror="onCDNFailed()"></script>

Another way, is to just check after the CDN script if you have the resource you expect, and if not you can create a new script with different URL, something like this:

<script src="https://CDN/jquery.min.js"></script>
<script>
   window.jQuery || document.write('<script src="/your/jquery"></script>');
</script> 
about 4 years ago · Juan Pablo Isaza Report

0

To add the downloaded JavaScript / CSS file instead of using CDN you can replace your code with

<script type="text/javascript" src="./yourfilename.js"></script> for JS 
<link href="./yourfilename.css" rel="stylesheet"> for CSS  

Here change the yourfile name appropriately with downloaded file name. This is the appropriate way if this didn't work check with licensing and policy of library you are using. This will only work if both files are in same directory.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!