[英]Force load JS script on page
我有一個第三方 JS 腳本,其中定義了 class Oreole
。 該腳本位於別處的 CDN 服務器上。 我的 HTML 中有這個參考:
<script src="https://someothercdn.com/oreole.js"/>
稍后在我的頁面腳本代碼中我有
let oreole = new Oreole
有時 CDN 會因 504 或 502 而失敗,並且我的代碼會崩潰。 通常,頁面重新加載會有所幫助。 但是如何在我的頁面上強制重新加載腳本?
if (typeof(Oreole) == "undefined") {
//Do what exactly?
}
您可能應該在其他地方托管 oreole.js,但是如果您想使用不可靠的 cdn 訪問 go,您可以使用 jquery 執行類似的操作
$.getScript("https://someothercdn.com/oreole.js", function() {
// do everything that needs oreole.js here
});
如果找不到 oreole.js 或發生某些事情,頁面將至少保持運行,只有 oreole 部分會崩潰
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.