var StyleWriter = new Class({
// css classes on the fly, based on by Aaaron Newton's version
createStyle: function(css, id) {
try {
if (document.id(id) && id) return;
var style = new Element('style', {id: id||'',type:'text/css'}).inject(document.getElement('head'));
if (Browser.Engine.trident)
style.styleSheet.cssText = css;
else
style.set('text', css);
} catch(e) {
console.log("failed:", e);
}
}
});
var rules = [
"#application_osx { background: red url(/path/to/image/application_osx.png) }",
"#someotherid { background: url(/path/to/image/someotherid.png) }"
];
// direct use through class
(function() {
new StyleWriter().createStyle(rules.join("\n"), "mystyles");
}).delay(2000); // wait 2 sec then add css rules
// use as a mixin
var foo = new Class({
Implements: [StyleWriter, Options],
options: {
rulesId: "foorules",
rules: [
"#application_osx { background: green url(/path/to/image/application_osx.png) }",
"#someotherid { background: url(/path/to/image/someotherid.png) }"
]
},
initialize: function(options) {
this.setOptions(options);
this.createStyle(this.options.rules.join("\n"), this.options.rulesId);
}
});
new foo();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.