Created
September 3, 2012 04:41
-
-
Save samsargent/3606764 to your computer and use it in GitHub Desktop.
Lazy Loading those 3rd Party Social Widgets slowing down your site (from html5rocks.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.thirdParty = { | |
init: function() { | |
$('.share').before('<div class="share preview" onmouseover="thirdParty.load(this);"><img src="/static/images/share.png"></div>'); | |
}, | |
load: function(el) { | |
$(el).remove(); | |
thirdParty.plus1(); | |
thirdParty.twitter(); | |
thirdParty.facebook(); | |
$('.share').css('display', 'block'); | |
}, | |
facebook: function() { | |
$('li.facebook').not(':has("iframe")').html(function() { | |
var html = '<iframe src="//www.facebook.com/plugins/like.php?href='+ | |
this.getAttribute('data-url') + | |
'&layout=button_count&show_faces=false&width=80&action=like'+ | |
'&font=arial&colorscheme=light&height=21" scrolling="no" '+ | |
'frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;"'+ | |
' allowTransparency="true"></iframe>'; | |
return html; | |
}); | |
}, | |
twitter: function() { | |
$.ajax({ | |
url : '//platform.twitter.com/widgets.js', | |
dataType : 'script', cache: true, | |
success : function() { twttr.widgets.load(); } | |
}); | |
}, | |
plus1: function() { | |
// https://developers.google.com/+/plugins/+1button/#async-load | |
window.___gcfg = { lang: document.documentElement.lang, parsetags: 'explicit' }; | |
$.ajax({ | |
url : '//apis.google.com/js/plusone.js', | |
dataType : 'script', cache: true, | |
success : function(){ | |
gapi.plusone.go(); | |
gapi.plus.go(); | |
} | |
}); | |
} | |
}; | |
thirdParty.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment