Defer loading of social script after onload


/ Published in: JavaScript
Save to your folder(s)

The loading of JS social buttons its deferred until onload event its fired.


Copy this code and paste it in your HTML
  1. <script>
  2. (function(w, d, s) {
  3. function go(){
  4. var js, fjs = d.getElementsByTagName(s)[0], load = function(url, id) {
  5. if (d.getElementById(id)) {return;}
  6. js = d.createElement(s); js.src = url; js.id = id;
  7. fjs.parentNode.insertBefore(js, fjs);
  8. };
  9. load('//connect.facebook.net/en_US/all.js#appId=272697932759946&xfbml=1', 'fbjssdk');
  10. load('https://apis.google.com/js/plusone.js', 'gplus1js');
  11. load('//platform.twitter.com/widgets.js', 'tweetjs');
  12. }
  13. if (w.addEventListener) { w.addEventListener("load", go, false); }
  14. else if (w.attachEvent) { w.attachEvent("onload",go); }
  15. }(window, document, 'script'));
  16. </script>

URL: http://www.aaronpeters.nl/blog/why-loading-third-party-scripts-async-is-not-good-enough

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.