Last Updated: February 25, 2016
·
944
· pniemczyk

Css Injector by Javascript (CoffeeScript)

When you need make css preview online you can use that

example

coffeescript function code cod


cssStyleInjector = (cssText, id="default", elem="head") ->
  thisCss = document.getElementById(id)
  return thisCss.innerHTML = cssText  if thisCss?
  css = document.createElement("style")
  css.setAttribute "type", "text/css"
  css.setAttribute "id", id
  style = document.createTextNode(cssText)
  css.appendChild style
  document[elem].appendChild css

javascript function code


var cssStyleInjector;

cssStyleInjector = function(cssText, id, elem) {
  var css, style, thisCss;
  if (id == null)   { id = "default"; }
  if (elem == null) {  elem = "head"; }
  thisCss = document.getElementById(id);
  if (thisCss != null) {  return thisCss.innerHTML = cssText; }
  css = document.createElement("style");
  css.setAttribute("type", "text/css");
  css.setAttribute("id", id);
  style = document.createTextNode(cssText);
  css.appendChild(style);

  return document[elem].appendChild(css);
};