So for a site I maintain, InsideCCS, I have the following code for dynamically inserting stylesheets on a page:
- var newCSS = [];
- $.each(options.styles, function(index) {
- newCSS.push('<link href="/styles/'+options.styles[index]['href']+'" media="screen" rel="stylesheet" type="text/css" class="dynamic_css" />');
- });
- $('head').append(newCSS.join(""));
- if ($.browser.msie) { $('head').html($('head').html()); }
I've tried refactoring this in various ways but all to no avail. For instance,
- $.each(options.styles, function(index) {
- $('<link />', {
- href: '/styles/' + options.styles[index]['href'],
- media: 'screen',
- rel: 'stylesheet',
- type: 'text/css',
- 'class': 'dynamic_css'
- }).appendTo('body');
- });
Any ideas?