How to load local copy of bootstrap css when the cdn server is down?

自闭症网瘾萝莉.ら 提交于 2019-12-18 12:19:22

问题


As of now I'm using like this for javascript

<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js"></script>
<script>    $.fn.modal || document.write('<script src="js/bootstrap.min.js">\x3C/script>')</script>

I'm loading bootstrap.css from bootstrapcdn like this

<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet">

Can someone tell me how to load local copy if the cdn server is down.?


回答1:


My solution for this was to have an empty div using the bootstrap hidden class style at the top of the body:

<div id="bootstrapCssTest" class="hidden"></div>

And then to test it later with Javascript and append it to the head if the div is visible:

    <script type="text/javascript">

    if ($('#bootstrapCssTest').is(':visible') === true) {
        $('<link href="/localcopy/css/bootstrap.css" rel="stylesheet" type="text/css" />').appendTo('head');
    }
</script>



回答2:


Would performing a jquery.Get() on the file work? Depending on the result, you would know whether it's available or not. And seeing as it occurs on the clientside, local caching would make this a non-issue for the extra bandwidth.




回答3:


This gist has a code snippet that I found particularly useful for detecting the need to load a CSS fallback.

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" />

<script type="text/javascript">
    function cssLoaded(href) {
        var cssFound = false;

        for (var i = 0; i < document.styleSheets.length; i++) {
            var sheet = document.styleSheets[i];
            if (sheet['href'].indexOf(href) >= 0 && sheet['cssRules'].length > 0) {
                cssFound = true;
            }
        };

        return cssFound;
    }

    if (!cssLoaded('bootstrap-combined.min.css')) {
        local_bootstrap = document.createElement('link');
        local_bootstrap.setAttribute("rel", "stylesheet");
        local_bootstrap.setAttribute("type", "text/css");
        local_bootstrap.setAttribute("href", "/Content/Styles/bootstrap-combined.min.css");
        document.getElementsByTagName("head")[0].appendChild(local_bootstrap);
    }
</script>

just replace /Content/Styles/bootstrap-combined.min.css with the correct path to your local css and you should be good




回答4:


Try this,

<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<script>
    function cssLoaded(href) {
        var cssFound = false;
        for (var i = 0; i < document.styleSheets.length; i++) {
            var sheet = document.styleSheets[i];
            if (
            sheet['href'] == "http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" ||
            sheet['href'] == "https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css") {
                cssFound = true;
            }
        };
        return cssFound;
    }
    if (!cssLoaded('bootstrap.min.css')) {
        local_bootstrap = new CustomEvent('link');
        local_bootstrap.setAttribute("rel", "stylesheet");
        local_bootstrap.setAttribute("href", "/css/bootstrap.min.css");
        document.getElementsByTagName("head")[0].appendChild(local_bootstrap);
    }
</script>

Source



来源:https://stackoverflow.com/questions/14141532/how-to-load-local-copy-of-bootstrap-css-when-the-cdn-server-is-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!