Here's a solution that will not be left to future maintainers, asks what the code actually does:
function setSheetContent( sheet, text ) { if(sheet.styleSheet) sheet.styleSheet.cssText = text; else sheet.innerHTML = text; } var sheet = document.createElement('style'); sheet.type = 'text/css'; setSheetContent( sheet, data.style );
or wrap it for even more convenience (if you never want to modify the contents of an existing sheet)
function stylesheetWithContent( sheet, text ) { var sheet = document.createElement('style'); sheet.type = 'text/css'; if(sheet.styleSheet) sheet.styleSheet.cssText = text; else sheet.innerHTML = text; return sheet; } var sheet = stylesheetWithContent( data.style );
source share