Last Updated: February 25, 2016
·
1.186K
· Paweł Niemczyk

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);
};

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside