Tuesday, December 27, 2011

Dynamically Adding a Style Sheet

I've been looking at some of Google's code for their Html5Slides project and like the way they dynamically link in a style sheet:
var el = document.createElement('link');
  el.rel = 'stylesheet';
  el.type = 'text/css';
  el.href = 'http://www.Example.com/...' + 'styles.css';
  document.body.appendChild(el);
I think they code it this way so people using their HTML5 slide template don't have to include a bunch of extra css files, just this one JavaScript file. If people get the file dynamically from Google all the time, Google can change the styles without breaking people's links. It's just one less dependency.

No comments: