在页面上强制加载JS脚本

时间:2020-11-12 12:05:07

标签: javascript

我有一个第三方JS脚本,其中定义了类Oreole。该脚本位于CDN服务器上的其他位置。我的HTML中有此参考文献:

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

稍后我的页面脚本代码中有

let oreole = new Oreole

有时CDN失败,并显示504或502,我的代码崩溃了。通常,页面重新加载会有所帮助。但是如何强制在页面上重新加载脚本?

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

1 个答案:

答案 0 :(得分:0)

您可能应该在其他地方托管oreole.js,但如果要使用不可靠的CDN,则可以使用jquery做类似的事情

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

如果未找到oreole.js或仅发生某些事情,页面将继续至少运行