Posted By

jefrancomix on 05/07/13


Tagged

onload twitter facebook googleplus


Versions (?)

Defer loading of social script after onload


 / Published in: JavaScript
 

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

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

  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>

Report this snippet  

You need to login to post a comment.