Last active
May 13, 2019 14:24
-
-
Save ardianta/2cffbc578d73aea1ce0a87f65f0e5822 to your computer and use it in GitHub Desktop.
Disqus Lazyloading using IntersectionObserver
This file contains 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
<div id="disqus_thread">Loading Comments...</div> | |
<script> | |
var disqus_config = function () { | |
this.page.url = "https://www.petanikode.com/flutter-web"; | |
this.page.identifier = "flutter-web"; | |
this.page.title = "Tutorial Membuat Web dengan Flutter"; | |
}; | |
</script> |
This file contains 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
document.addEventListener("DOMContentLoaded", function () { | |
let disqusThread = document.querySelector("#disqus_thread"); | |
if ("IntersectionObserver" in window) { | |
if (disqusThread != null) { | |
new IntersectionObserver((entries, observe) => { | |
entries.forEach(entry => { | |
if (entry.isIntersecting) { | |
let d = document, s = d.createElement('script'); | |
s.src = '//petanikode.disqus.com/embed.js'; | |
s.async = true; | |
s.setAttribute('data-timestamp', +new Date()); | |
(d.head || d.body).appendChild(s); | |
observe.unobserve(entry.target); | |
} | |
}); | |
}).observe(disqusThread); | |
} | |
} else { | |
// Possibly fall back to a more compatible method here | |
console.log("Browser Not supported, plase upgrade!"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment