Last active

Embed URL


SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Loading multiple 3rd party widgets asynchronously

View multiple-3rd-party-widgets.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
(function() {
var script,
scripts = document.getElementsByTagName('script')[0];
function load(url) {
script = document.createElement('script');
script.async = true;
script.src = url;
scripts.parentNode.insertBefore(script, scripts);

If there's no previous script tag on the page, won't scripts.parentNode throw a (Cannot read property parentNode of undefined) error ?

also any for in loop should use hasOwnProperty to protect for any prototype modifications on the page.


1) You're always going to have a script tag on the page, because you need to add the script above somehow

Very true! Good point =)

You also might want to wrap this in a function to protect the variable namespaces.

As you don't use the object keys, maybe it would be better to loop an array instead (performance).

common approach.


How about now?




})(); ?

@harry1989 Both are valid Javascript. But if you want an opinionated answer, try Crockford

i use a window onload version. look

Similar to this one: , although i find that one much better.

How about to pass the "document" as parameter into scope?



i think pass 'document' into scope is better

ghprod commented

Nice :+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.