Use local css file if link to online css file is dead

前端 未结 3 445
一个人的身影
一个人的身影 2021-01-23 09:32

The problem:


I am using w3.css(if you enter now w3schools.com site is down for maintenance) for styling my website.The problem

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-23 10:04

    Why not use JS like this?

    
    

    $.each(document.styleSheets) iterates through the styleSheets of the document. Check out https://developer.mozilla.org/en-US/docs/Web/API/Document/styleSheets for more info.

    Then we specifically check if http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.css is accessible. We use the ternary operator var rules = sheet.ruless ? sheet.rules : sheet.cssRules; The ternary operator works similar to an if statement, rules becomes sheet.rules if sheet.rules evaluates to true. If it is false it will become sheet.cssRules.

    Lastly we check if there's anything in rules. If there isn't, we fallback to our local stylesheet solution and add it to the of the document.

    Although you may run into issues when using a very slow to load CDN. Alternatively you could use the onerror event

提交回复
热议问题