Skip to content

Instantly share code, notes, and snippets.

@feliperohdee
Created July 23, 2019 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feliperohdee/c32a038a214d299eb855f4c7cbeb531b to your computer and use it in GitHub Desktop.
Save feliperohdee/c32a038a214d299eb855f4c7cbeb531b to your computer and use it in GitHub Desktop.
on demand script loader
export const loadScript = (id, url, test) => new Promise(resolve => {
let s = document.getElementById(id);
if (!s) {
s = document.createElement('script');
s.setAttribute('id', id);
s.defer = true;
s.src = url;
document.body.appendChild(s);
}
const doTest = () => {
test(s) ? resolve() : setTimeout(doTest, 250);
};
doTest();
});
// how to use
loadScript('pagseguro', 'https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js', () => !!window.PagSeguroDirectPayment)
.then(() => {
// do something
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment