1
0
mirror of https://gitee.com/coder-xiaomo/algorithm-visualization synced 2025-01-25 10:50:30 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

优先加载CDN,失败后fallback为本地js

This commit is contained in:
程序员小墨 2022-05-18 21:39:05 +08:00
parent 8f4d77af6d
commit df1a0668a6

View File

@ -63,11 +63,38 @@
</div> -->
</div>
<script>
// CDN 资源加载失败时 fallback 使用本地js
// refer: https://blog.csdn.net/weixin_44326389/article/details/104041963
function errorload(elem) { //cdn错误
if (elem.getAttribute('data-errorTimes') == 1) {
elem.onerror = null; // 执行完 onerror 事件后,置 οnerrοr = null 来清除 onerror 事件。
return;
}
elem.setAttribute('data-errorTimes', 1);
var _src = elem.getAttribute('local-src');
loadScript(_src, elem);
}
function loadScript(_src, prevEl) { //引入本地js
var _el = document.createElement('script');
_el.src = _src;
if (prevEl) {
prevEl.parentNode.insertBefore(_el, prevEl.nextSibling);
} else {
document.body.appendChild(_el);
}
}
</script>
<!-- D3.js refer: https://d3js.org/ -->
<script src="./assets/lib/d3/7.4.4/d3.min.js"></script>
<script src="https://cdn.staticfile.org/d3/7.4.4/d3.min.js" local-src="./assets/lib/d3/7.4.4/d3.min.js"
onerror="errorload(this);"></script>
<!-- GSAP refer: https://greensock.com/docs/v3/Installation -->
<script src="./assets/lib/gsap/3.10.4/gsap.min.js"></script>
<script src="https://cdn.staticfile.org/gsap/3.10.4/gsap.min.js" local-src="./assets/lib/gsap/3.10.4/gsap.min.js"
onerror="errorload(this);"></script>
<!-- class -->
<script src="./assets/js/class.js"></script>