https://github.com/pirxpilot/dynload
Basic script loader
https://github.com/pirxpilot/dynload
Last synced: 5 months ago
JSON representation
Basic script loader
- Host: GitHub
- URL: https://github.com/pirxpilot/dynload
- Owner: pirxpilot
- Created: 2013-10-11T14:22:53.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2018-11-21T15:55:12.000Z (over 7 years ago)
- Last Synced: 2025-03-11T21:38:55.333Z (over 1 year ago)
- Language: Makefile
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# load
Basic script loader.
The simplest and fastest way of loading scripts in modern browser is simply [listing them at the
end of the body element][1].
Use load only if for some reason you need to load your scripts dynamically - useful when
integrating various 3rd party widgets and snippets (google analytics, disqus, twitter API etc.) on
your page.
## Installation
Install with [npm]:
$ npm install dynload
## API
```javascript
var load = require('dynload');
load('//topic.disqus.com/count.js');
```
Please note that scripts added to the DOM are `async` by default (see this [article][1] on script
loading) which means that they will start executing as soon as they load in pretty much random
order.
You can force async to `false` but some browsers will ignore it.
```javascript
load('//topic.disqus.com/count.js', false);
```
`load` returns [script element](https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement) - you can register `onerror` and `onload` callbacks
```javascript
load('//cdn/script.js').onload = function () {
console.log('my script loaded');
});
```
and:
```javascript
load('//cdn/script.js').onerror = function (err) {
console.log('cannot load script', err);
});
```
If you your callback work with IE use [segmentio/script-onload](https://github.com/segmentio/script-onload)
```javascript
var load = require('dynload');
var onload = require('script-onload');
onload(load('//cdn/script.js'), function(err) {
if (err) {
console.log('Cannot load script', err);
} else {
console.log('Script loaded');
}
});
```
## License
MIT
[1]: http://www.html5rocks.com/en/tutorials/speed/script-loading/
[npm]: https://www.npmjs.org/