简体   繁体   English

HTML if语句,用于在CDN失败时加载本地JS / CSS

[英]HTML if statement to load local JS/CSS upon CDN failure

When loading a CSS/JS file from CDN or any external server, it is possible (even with low probability) to miss the file due to external failure. 从CDN或任何外部服务器加载CSS / JS文件时,由于外部故障,有可能(即使可能性很小)丢失该文件。 In this case, the html page will be corrupted in the lack of appropriate CSS and JS. 在这种情况下,缺少适当的CSS和JS会损坏html页面。

Is there a practical approach to load a local version upon CDN failure? 有没有可行的方法来在CDN失败时加载本地版本?

<link rel="stylesheet" href="http://cdn.com/style.css" type="text/css" />
IF (not loaded style.css){
<link rel="stylesheet" href="/css/style.css" type="text/css" media="screen" />
}

It would be easier to do this for JS, as we can test a JS function (provided in the JS file); 对于JS而言,这样做更加容易,因为我们可以测试JS函数(在JS文件中提供); then, loading the local file upon failure. 然后,在失败时加载本地文件。 For example, testing, if jQuery library is available. 例如,测试jQuery库是否可用。

However, I am curious to know if there is a practical method for this! 但是,我很想知道是否有可行的方法!

I would do it this way. 我会这样做。

Create a class within your stylesheet ui-helper-hidden and then add a div as the first element on your page; ui-helper-hidden样式表中创建一个类,然后将div作为页面上的第一个元素添加;

<body><div class="ui-helper-hidden"></div><!-- rest of html --></body>

After you have checked to make sure your CDN javascript file has been loaded, then use this bit of code note i am using jquery 在检查以确保您的CDN javascript文件已加载后,请使用以下代码说明我正在使用jquery

<script>
    // CSS Fallback
    $(function () {
        if ($('.ui-helper-hidden:first').is(':visible') === true) {
            $('<link rel="stylesheet" type="text/css" href="/pathtocss/nameofstylesheet.css" />').appendTo('head');
        }
    });
</script>

This will check to see if the element which should be hidden is or not. 这将检查要隐藏的元素是否存在。 If it isnt hidden, then you know your css file has not loaded from the CDN. 如果未隐藏,则说明您的CSS文件尚未从CDN加载。

I use this method for jQuery and jQuery UI via a CDN 通过CDN将这种方法用于jQuery和jQuery UI

For Javascript, you can listen for the onload and onerror events when building a dynamic script. 对于Javascript,可以在构建动态脚本时侦听onloadonerror事件。 However, in those same pages, it shows otherwise for CSS. 但是,在这些页面中,对于CSS则显示其他内容。

The only reliable way to dynamically load CSS is to do it via AJAX. 动态加载CSS的唯一可靠方法是通过AJAX进行。 You could load the styles via dynamic link tags but without those events, you won't know if they have been loaded at all. 可以通过动态链接标签加载样式,但是如果没有这些事件,则根本无法知道是否已加载样式。 You could poll for the styles, but it's hackish IMO. 您可以轮询样式,但是它是不可靠的IMO。

Another way to do it is make the server read those CDN files. 另一种方法是使服务器读取这些CDN文件。 If they're good, print the urls for the links. 如果效果很好,请打印链接的网址。 But if those links are dead, make it print the local urls instead. 但是,如果这些链接无效,则改为打印本地网址。 This would be more reliable, and offloads your logic to the server. 这将更加可靠,并将您的逻辑转移到服务器上。 This assumes that you have access to the server. 这假设您有权访问服务器。

Or better, use the local versions in the first place! 或者更好的是,首先使用本地版本! With good caching, bandwidth won't be an issue 有了良好的缓存,带宽就不会成为问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用本地 css 和 js 在 WebView 中加载 HTML - Load HTML in WebView with local css and js 加载CDN上托管的JS库的本地版本 - Load local versions of the JS libraries hosted on a CDN 在 React Native Webview 中使用本地 css、js 加载本地 HTML - Load Local HTML with local css, js in React Native Webview 页面加载时,本地 State 变量不会在 Next.js 中更改 CSS? - Local State variable not changing CSS in Next.js upon page load? 从CDN加载脚本或CSS文件,但仍具有本地备份 - Load scripts or css files from a CDN and still have local backup 如果 CDN 不工作,有没有办法加载内部下载的 js 和 css 文件 - Is there a way to load internally downloaded js and css files if CDN isnt working 如何在 Wordpress 插件中加载多个 css、js 和 CDN - How to load multiple css, js and CDN in Wordpress plugin HTML,CSS,JS if语句认为1 == 0 - HTML, CSS, JS if statement thinks 1==0 如何拦截 WKWebView 请求以检测哪些本地资源文件(css、js、png、...)与 HTML 文件一起加载? - How to intercept a WKWebView request to detect which local resource files (css, js, png, …) load together with a HTML file? 当CDN无法加载时,为font-awesome附加本地CSS回退 - Appending a local CSS fallback for font-awesome when the CDN fails to load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM