postscribe-script

1.1.0 • Public • Published

postscribe-script

Build Status

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.

Dependencies (2)

Dev Dependencies (11)

Package Sidebar

Install

npm i postscribe-script

Weekly Downloads

0

Version

1.1.0

License

MIT OR GPL-3.0+

Last publish

Collaborators

  • futpib