Reputation: 1779
I have read up on it and it seems like the best method to add a stylesheet using jQuery is the following:
$('head').append('<link rel="stylesheet" type="text/css" href="../css/mystyle.css">');
Now what I would like to do, is switch between different themes using this method:
<script type="text/javascript">
$(document).ready(function () {
if($('body').hasClass('pink'))
{
$('head').append('<link rel="stylesheet" type="text/css" href="../css/mystylePink.css">');
}
else
{
$('head').append('<link rel="stylesheet" type="text/css" href="../css/mystyle.css">');
}
});
</script>
MY QUESTION: Apart form users not having javascript enabled, what complications/issues exists when loading a stylesheet like this? IE? Mobile browsers? Since I actually want to use this on a mobile site...
Upvotes: 3
Views: 7056
Reputation: 16961
The main problem here would be the flash of unstyled content while the stylesheet loads, rather than IE or mobile browsers.
I suggest you use a CSS preprocessor (SASS, LESS) and just use one stylesheet with your normal styles and then override with the .pink
class underneath.
.myClass{
color: white;
background: black;
}
.pink .myClass{
color: pink;
background: grey;
}
Upvotes: 2
Reputation: 61
This method isnt't working in IE8 and below.
You have to use another method, like IEss document.createStyleSheet() method.
See this discussion for more information: Can I load external stylesheets on request?
Upvotes: 1