https://github.com/caiogondim/resilient.js
Resilient JS loader
https://github.com/caiogondim/resilient.js
Last synced: 3 months ago
JSON representation
Resilient JS loader
- Host: GitHub
- URL: https://github.com/caiogondim/resilient.js
- Owner: caiogondim
- Created: 2014-12-20T17:12:46.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-21T02:26:40.000Z (over 10 years ago)
- Last Synced: 2025-01-24T18:08:37.780Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 238 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Resilient.js
resilience
> the capacity to recover quickly from difficulties; toughness.Resilient.js is a script loader proof-of-concept made to load a given JS file
from different URLs. There is no perfect CDNs, and there are better CDNs for
each country or continent. With that lib, we try to load a JS file from
different CDNs, until we succesfully download the file.## Usage
Resilient.js should be the first script to be loaded in your page.
Instantiate it like so```js
if (Resilient.isSupported()) {
var resilient = new Resilient();
resilient.init();
}
```In your `` tag, use the data attributes `data-alt-src` and `data-module`
```html
<script
src="https://code.jquery.com/jquery-2.1.3.min.js"
data-module="jquery"
data-alt-src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"
>
``````js
resilient.on('load', ['jquery'], function() {
console.log('jQuery loaded');
// From now on it is safe to use your code that depends on jQuery
});
```If something goes wrong while loading a module, an `error` event will be fired.
And another try to load the script will be made using the `data-alt-src`.```js
resilient.on('error', ['jquery'], function() {
console.log('Error while loading jQuery')
});
```It always possible to listen to all `load` and `error` events using the `*`
wildcard.```js
resilient
.on('error', ['*'], function(moduleName) {
console.log('Error while loading ' + moduleName);
})
.on('load', ['*'], function(moduleName) {
console.log(moduleName + ' loaded');
});
```## API
### `on`
```js
instance.on('eventName', ['moduleName1', 'moduleName2'], callback);
```### `off`
```js
instance.off('eventName', ['moduleName1', 'moduleName2']);
```### `trigger`
```js
instance.trigger('eventName', ['moduleName1', 'moduleName2']);
```## Events
### `load`
Triggered when a script tag was loaded.
### `error`
Triggered when there was an error loading a script.
## Browser Support
 |  |  |  | 
--- | --- | --- | --- | --- |
Latest ✔ | Latest ✔ | 11+ ✔ | Latest ✔ | 6.0+ ✔ |## Credits
- Icon by [Stephen Borengasser](http://thenounproject.com/term/resilient/38883/)