2

I need to fetch the custom-style_1.css if its found otherwise i need to fetch the custom-style.css.

<link rel="stylesheet" type="text/css" href="lib/css/custom-style_1.css"/>
<link rel="alternate stylesheet" type="text/css" href="lib/css/custom-style.css"/>
Karthik
  • 77
  • 10
  • I guess [this](http://wordpress.stackexchange.com/questions/121273/how-to-check-if-a-stylesheet-is-already-loaded) may help you. – StackUseR Nov 24 '16 at 12:22
  • http://stackoverflow.com/a/7452378/5284695 – Aakash Martand Nov 24 '16 at 12:24
  • I tried based on the below site example http://edutechwiki.unige.ch/en/CSS_media_and_alternative_style_sheets_tutorial is this possible – Karthik Nov 24 '16 at 12:28
  • Why don't you load `custom-style.css` first, and then `custom-style_1.css` will override it if it is loaded successfully. –  Nov 24 '16 at 13:34

1 Answers1

1

You could fetch your stylesheet via ajax and return the corresponding link if it succeeds or fails:

$.ajax({
    url: 'lib/css/custom-style_1.css',
    success: function() {
        $('head').append('<link  rel="stylesheet" href="lib/css/custom-style_1.css" />');
    },
    error: function() {
        $('head').append('<link  rel="stylesheet" href="lib/css/custom-style.css" />');
    }
});
marcobiedermann
  • 4,317
  • 3
  • 24
  • 37
  • I tried based on the below site example http://edutechwiki.unige.ch/en/CSS_media_and_alternative_style_sheets_tutorial is this possible – Karthik Nov 24 '16 at 12:30
  • This has nothing to do with your question. You can set a media attribute on which the stylesheed will be loaded. You question was to load another stylesheed, if the first one fails to load. – marcobiedermann Nov 24 '16 at 12:31
  • This could cause CORS problems if the first stylesheet is on another domani. –  Nov 24 '16 at 13:31
  • You are right, but this depends on the script. Otherwise you have to allow cross-origin resources – marcobiedermann Nov 24 '16 at 13:32