Forked from CrocoDillon/Lazy load Disqus comments.js
Created
October 20, 2018 00:10
-
-
Save samdoidge/471533ad3aa6bd6e21438356252ec08f to your computer and use it in GitHub Desktop.
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
/* | |
* http://christian-fei.com/tutorials/how-to-lazy-load-disqus-comments/ | |
* | |
* <div class="comments"></div> | |
*/ | |
var comments = document.getElementsByClassName('comments')[0], | |
disqusLoaded = false; | |
function loadDisqus() { | |
var disqus_shortname = 'your_disqus_shortname'; | |
var dsq = document.createElement('script'); | |
dsq.type = 'text/javascript'; | |
dsq.async = true; | |
dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js'; | |
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); | |
disqusLoaded = true; | |
} | |
//Get the offset of an object | |
function findTop(obj) { | |
var curtop = 0; | |
if (obj.offsetParent) { | |
do { | |
curtop += obj.offsetTop; | |
} while (obj = obj.offsetParent); | |
return curtop; | |
} | |
} | |
if(window.location.hash.indexOf('#comments') > 0) | |
loadDisqus(); | |
if(comments) { | |
var commentsOffset = findTop(comments); | |
window.onscroll = function() { | |
if(!disqusLoaded && window.pageYOffset > commentsOffset - 1000) | |
loadDisqus(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still works well. I've changed http to https.