import.js - Importing JavaScript files.

About

It is JavaScript to import JavaScript files.
When a pass of import.js is right, that's imported according to the hierarchy.

Import test

    Import test on lower page

    Download

    dounokouno/import.js - GitHub

    Usage

    File composition

    index.html
    2nd/
      + index.html
      + 3rd/
        + index.html
    js/
      + jquery.js
      + import.js
      + external-1.js
      + external-1.js

    HTML

    <script type="text/javascript" src="js/import.js"></script>

    2nd page HTML

    <script type="text/javascript" src="../js/import.js"></script>

    3nd page HTML

    <script type="text/javascript" src="../../js/import.js"></script>

    JavaScript

    var require = function(jsfile) {
      var ary = document.getElementsByTagName('script');
      for (var i=0;i<ary.length;i++) {
        if (ary[i].getAttribute('src').match(/import\.js$/)) {
          var jspath = ary[i].getAttribute('src').match(/.+\//);
        }
      }
      var elem = document.createElement('script');
      elem.setAttribute('type', 'text/javascript');
      elem.setAttribute('src', jspath+jsfile);
      document.getElementsByTagName('head')[0].appendChild(elem);
    }
    
    // Add the following
    require('external-1.js');
    require('external-2.js');

    Lisence

    Dual licensed under the MIT or GPL Version 2 licenses.

    History

    • 2011-01-31 : Opening to the public.