Giter VIP home page Giter VIP logo

resloader's Introduction

resLoader

A JavaScript resource loader featuring conditional loading, asynchronous- or sequential loading

Build Status GitHub

Installation

Using npm

npm install @myspace-nu/resloader --save

Using CDN

<script src="https://cdn.jsdelivr.net/npm/@myspace-nu/resloader/dist/resLoader.min.js"></script>

Or include the script manually

<script src="/path/to/resLoad.min.js"></script>

Usage

Simple usage

<script type="text/javascript">
	var loader = new resLoader();
	loader.load("myScript.js");
</script>

Now, that isn't very impressive. Let's load some more scripts and add a callback when all scripts are loaded.

<script type="text/javascript">
	var loader = new resLoader();
	loader.load({
		url: ["myScript.js","anotherScript.js"],
		onComplete: function(){
			console.log("All done");
		}
	});
</script>

Let's say that one script is depending on another to be loaded first, simply set async to false (default true).

<script type="text/javascript">
	var loader = new resLoader();
	loader.load({
		url: ["myScript.js","myScript-extra.js"],
		async: false,
		onComplete: function(){
			console.log("All done, and in the right order");
		}
	});
</script>

You can also add conditions for the resource to be loaded. Like, only load jQuery unless it is already loaded.

<script type="text/javascript">
	var loader = new resLoader();
	loader.load({
		url: "//code.jquery.com/jquery-3.3.1.min.js",
		loadUnless: window.jQuery,
		async:false,
		onComplete: function(){
			$("body").append("<p>One more paragraph</p>");
		}
	});
</script>

Note that onComplete is called regardless of the test is evaluted to true or false, i.e. in the example the code will be executed even if jQuery has been loaded earlier on the page

If you include different resources to the same html out and there is a chance that several of the resources include for example jQuery you can solve this by loading these scripts in blocking mode like this.

<script src="/path/to/resLoad.min.js"></script>
<script type="text/javascript">
	var loader = new resLoader();
	loader.load([
		{ url:"//code.jquery.com/jquery-3.3.1.min.js", unless:window.jQuery, blocking:true }
	]);
</script>
...
<script type="text/javascript">
	var loader = new resLoader();
	loader.load([
		{ url:"//code.jquery.com/jquery-3.3.1.min.js", unless:window.jQuery, blocking:true }
	]);
</script>

In the example, jQuery will not be loaded a second time

Options

url - Resource urls to be loaded [array or string]

url: ["myScript.js","myScript-extra.js"]

async - Load the resource asynchronously [true/false]

async: false

Default: true

blocking - Load script in (render-)blocking mode [true/false]

blocking: true

Default: false

loadUnless - Load the resource unless expression is true

loadUnless: window.jQuery

unless - Shorthand for loadUnless

unless: window.jQuery

loadIf - Load the resource if expression is true

loadIf: window.jQuery

if - Shorthand for loadIf

if: window.jQuery

onComplete - Callback to be executed when everything is done (regardless of any conditional tests)

onComplete: function(){
	console.log("All done");
}

onLoad - Callback to be executed when a resource has beed loaded.

onLoad: function(){
	console.log("Resource has been loaded");
}

onLoadAll - Callback to be executed when all resources have beed loaded.

onLoadAll: function(){
	console.log("All resources are loaded");
}

resloader's People

Contributors

myspace-nu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.