2017 © Pedro Peláez
 

library script-loader

Script Js / Css loading without callbacks

image

santanas2b/script-loader

Script Js / Css loading without callbacks

  • Wednesday, March 21, 2018
  • by santanas2b
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • HTML
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

ScriptLoader

ScriptLoader is a Javascript library for async script loading with dependency tree., (*1)

Installation

Use the package manager bower to install ScriptLoader., (*2)

bower install santanas2b/script-loader

Usage

Load the javascript in your html

<script src="path_to_script_loader_folter/dist/scriptLoader.min.js" type="text/javascript"></script>

Initialize scriptLoader object

var scriptLoader = new ScriptLoader();

Add yours scripts

// add one script
scriptLoader.add({
    src: "https://ajax.googleapis.com/ajax/libs/d3js/5.12.0/d3.min.js"
});
// add many scripts
scriptLoader.add([
    {
        src: "js/custom.js",
        callback: function(){
            // Callback has called when script is loaded
        }
    },
    {
        src: "css/custom.css"
    },
    {
        src: "js/toto.js"
    }
]);

You can add script with requirements

When you add requirements , theys sources loaded before your script., (*3)

    scriptLoader.add({
        src: "js/toto.js",
        require: [
            {
                name: "owlCarousel",
                sources: [
                    {
                        src: "css/owl.css",
                        preload: true
                    },
                    {
                        src: "js/owl.js",
                        require: [
                            {
                                name: "jQuery",
                                sources: [
                                    {
                                        src:"https://ajax.googleapis.com/ajax/libs/d3js/5.12.0/d3.min.js"

                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ],
        callback: function(){
            // callback when script loaded
        }
    })

Last step : Launch the load

The Script Loader does a check not to add the same script multiple times. The loader script does not load the same script twice, even if you run the load function multiple times., (*4)

The loader script checks that a load is not running when launching the load function, so we can launch it after adding scripts, it will not impact performance., (*5)

    scriptLoader.load();

You can tell if the script already exists in the scriptLoader

    // This function return boolean
    scriptLoader.has({
        src: "toto.js"
    });

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change., (*6)

Please make sure to update tests as appropriate., (*7)

License

MIT, (*8)

The Versions

21/03 2018

dev-master

9999999-dev

Script Js / Css loading without callbacks

  Sources   Download

by Avatar santanas2b