Skip to content
Advertisement

Force load JS script on page

I have a third party JS script with class Oreole defined there. This script is located on a CDN server elsewhere. I have this reference in my HTML:

<script src="https://someothercdn.com/oreole.js"/>

Later in my page script code I have

let oreole = new Oreole

Sometimes the CDN fails with 504 or 502 and my code crashes. Usually, page reload helps. But how do I force script reload on my page?

if (typeof(Oreole) == "undefined") {
  //Do what exactly?
}

Advertisement

Answer

You should probably host oreole.js somewhere else, but if you want to go with unreliable cdn, you can do something like this using jquery

$.getScript("https://someothercdn.com/oreole.js", function() {
 // do everything that needs oreole.js here
});

The page will keep running atleast, if oreole.js is not found or something happens only oreole part will crash

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement