Loading multiple 3rd party widgets asynchronously
(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); | |
} | |
load('//apis.google.com/js/plusone.js'); | |
load('//platform.twitter.com/widgets.js'); | |
load('//s.widgetsite.com/widget.js'); | |
}()); |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Very true! Good point =) You also might want to wrap this in a function to protect the variable namespaces. |
This comment has been minimized.
This comment has been minimized.
As you don't use the object keys, maybe it would be better to loop an array instead (performance). |
This comment has been minimized.
This comment has been minimized.
common approach. |
This comment has been minimized.
This comment has been minimized.
How about now? |
This comment has been minimized.
This comment has been minimized.
Shouldn't }()); be })(); ? |
This comment has been minimized.
This comment has been minimized.
@harry1989 Both are valid Javascript. But if you want an opinionated answer, try Crockford http://www.youtube.com/watch?v=taaEzHI9xyY&feature=youtu.be#t=33m39s |
This comment has been minimized.
This comment has been minimized.
i use a window onload version. look https://gist.github.com/jswebschmiede/3660918 |
This comment has been minimized.
This comment has been minimized.
Similar to this one: https://gist.github.com/necolas/1025811 , although i find that one much better. |
This comment has been minimized.
This comment has been minimized.
How about to pass the "document" as parameter into scope? |
This comment has been minimized.
This comment has been minimized.
good |
This comment has been minimized.
This comment has been minimized.
goood |
This comment has been minimized.
This comment has been minimized.
i think pass 'document' into scope is better |
This comment has been minimized.
This comment has been minimized.
Nice |
This comment has been minimized.
This comment has been minimized.
ty babe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
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.