Skip to content

Instantly share code, notes, and snippets.

@beau-gosse
Created January 7, 2016 18:29
Show Gist options
  • Save beau-gosse/0815e64ccf4efbc17ea3 to your computer and use it in GitHub Desktop.
Save beau-gosse/0815e64ccf4efbc17ea3 to your computer and use it in GitHub Desktop.
Lazy load Disqus with JQuery -- Murat Corlu's method.
// # Lazy load Disqus w/ Murat Corlu's method.
var disqus_div = $("#disqus_thread");
if (disqus_div.size() > 0) {
var disqus_loaded = false,
// Where to start loading
top = disqus_div.offset().top,
disqus_data = disqus_div.data(),
check = function() {
if (!disqus_loaded && $(window).scrollTop() + $(window).height() > top) {
disqus_loaded = true;
for (var key in disqus_data) {
if (key.substr(0, 6) == 'disqus') {
// Get Disqus parameters via data attributes
window['disqus_' + key.replace('disqus', '').toLowerCase()] = disqus_data[key];
}
}
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = 'http://' + window.disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}
};
$(window).scroll(check);
check();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment