postscribe-script
v1.1.0
Published
Asynchronously add script tag, with Promise, resolve after script executes
Downloads
4
Readme
postscribe-script
Asynchronously add script
tags with krux/postscribe.
Returns a Promise that is resolved after script executes.
Useful with browserify/webpack.
Usage
const postscribeScript = require('postscribe-script');
// add a `document.write(...)` ad script
postscribeScript('#ad', 'https://example.com/document-writer.js').then(() => {
// script was loaded and evaluated, and presumably wrote something inside #ad
});
// load jQuery dynamically
postscribeScript('https://code.jquery.com/jquery-git.min.js').then(() => {
// script was loaded and evaluated, we can use whatever it exports
$('body').fadeIn();
});
// something more realistic
postscribeScript('https://api-maps.yandex.ru/2.1/')
.then(() => ymaps.ready())
.then(() => {
const map = new ymaps.Map('map', {
center: [55.751574, 37.573856],
zoom: 9
});
});
Install
With npm do:
npm install postscribe-script
You will also need a Promise/A+ polyfill if you want to support older browsers. One of them is taylorhakes/promise-polyfill (recommended by github/fetch). Personally I like core-js, it has everything ES6 and more.