JSFiddle - React, Tailwind, and code Playground

by darcyclarke

JavaScript

var App = {};
App.resources = {};

// Create Unique ID
App.createID = function(){
    return ((new Date()).getTime() + "" + Math.floor(Math.random() * 1000000)).substr(0, 18);
};

// Script Loader
App.load = function(type, url, callback){
    var resource = {}, el = '', loadInHead = true;
    if(typeof callback !== 'function'){
        callback = function(){};
    }
    if(type === 'js'){
        el = 'script';
        type = 'text/javascript';
    } else if(type === 'css'){
        el = 'link';
        type = 'text/css';
    } else if(type === 'iframe'){
        el = 'iframe';
        type = '';
        loadInHead = false;
    }
    var resource = App.resources[App.createID()] = document.createElement(el);
    resource.loaded = false;
    resource.type = type;
    resource.style.display = "none";
    resource.src = resource.href = url;
    resource.onload = function(){
        if(!this.loaded){
            this.loaded = true;
            callback(this);
        }
    };
    resource.onreadystatechange = function(){
        if(!this.loaded && (this.readyState === 'complete' || this.readyState === 'loaded')){
            this.loaded = true;
            callback(this);
        }
    };
    if(loadInHead){
        document.getElementsByTagName('head')[0].appendChild(resource);
    } else {
        document.getElementsByTagName('body')[0].appendChild(resource);
    }
};

App.load('js', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js', function(){
    console.log(jQuery);
});