MASNathan
9/25/2013 - 10:08 AM

Awesome way to load dynamically some js and css files

Awesome way to load dynamically some js and css files

document.head.appendJs = document.body.appendJs = function( src, callback ) {
  script        = document.createElement('script');
  script.src    = src;
  script.onload = function() {
    callback();
  }
  this.appendChild(script);
};

document.head.appendCss = document.body.appendCss = function( src, callback ) {
  style        = document.createElement('link');
  style.href   = src;
  style.type   = 'text/css';
  style.rel    = 'stylesheet';
  style.onload = function() {
    callback();
  }
  this.appendChild(style);
};

document.head.appendCss('http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css', function() {
  document.head.appendJs('http://code.jquery.com/jquery-1.9.1.js', function() {
    document.body.appendJs('http://code.jquery.com/ui/1.10.3/jquery-ui.js', function() {
      console.log('jquery-ui loaded');
    });
  });
});