Skip to content

Instantly share code, notes, and snippets.

@marazmiki
Created April 6, 2020 03:22
Show Gist options
  • Select an option

  • Save marazmiki/423c39e03e427ceb6d27eb286ee5f83c to your computer and use it in GitHub Desktop.

Select an option

Save marazmiki/423c39e03e427ceb6d27eb286ee5f83c to your computer and use it in GitHub Desktop.

Revisions

  1. marazmiki created this gist Apr 6, 2020.
    31 changes: 31 additions & 0 deletions js-loader.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    const Loader = {
    require (scripts, callback) {
    this.loadCount = 0;
    this.callback = callback;
    this.totalRequired = scripts.length;
    scripts.forEach((src) => {
    this.writeScript(src)
    });
    },
    loaded (evt) {
    this.loadCount++;
    if (this.loadCount === this.totalRequired && typeof this.callback == 'function') {
    this.callback.call();
    }
    },
    writeScript (src) {
    const s = document.createElement('script');
    s.src = src;
    s.addEventListener('load', (e) => {
    this.loaded(e);
    }, false);
    document.querySelector('HEAD').appendChild(s);
    }
    };

    Loader.require([
    '{% static "admin/js/jquery.min.js" %}',
    '{% static "admin/js/jquery.init.js" %}',
    ], () => {
    console.log('ok, loaded jquery from admin');
    })