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

前端 未结 4 860
渐次进展
渐次进展 2020-12-30 06:04

As of now I\'m using like this for javascript


<         


        
相关标签:
4条回答
  • 2020-12-30 06:39

    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>
    
    0 讨论(0)
  • 2020-12-30 06:40

    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

    0 讨论(0)
  • 2020-12-30 06:58

    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

    0 讨论(0)
  • 2020-12-30 07:01

    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.

    0 讨论(0)
提交回复
热议问题