Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Loads Disqus comments on click with native JavaScript. Opens comments when linked to directly.
<div class="comments-block">
<button id="show-comments" onclick="disqus();return false;">Load Comments</button>
</div>
<div id="disqus_thread"></div>
<script>
var disqus_loaded = false;
var disqus_shortname = 'xxxx'; //Add your shortname here
function disqus() {
if (!disqus_loaded) {
disqus_loaded = true;
var e = document.createElement("script");
e.type = "text/javascript";
e.async = true;
e.src = "//" + disqus_shortname + ".disqus.com/embed.js";
(document.getElementsByTagName("head")[0] ||
document.getElementsByTagName("body")[0])
.appendChild(e);
//Hide the button after opening
document.getElementById("show-comments").style.display = "none";
}
}
//Opens comments when linked to directly
var hash = window.location.hash.substr(1);
if (hash.length > 8) {
if (hash.substring(0, 8) == "comment-") {
disqus();
}
}
//Remove this if you don't want to load comments for search engines
if(/bot|google|baidu|bing|msn|duckduckgo|slurp|yandex/i.test(navigator.userAgent)) {
disqus();
}
</script>
@ajaymalik14

This comment has been minimized.

Copy link

ajaymalik14 commented Mar 5, 2018

Is it really Increase website loading speed?

@bershatsky

This comment has been minimized.

Copy link

bershatsky commented Jan 14, 2020

This helps a lot! Thank you!!! I'm using it on my website on a JAMstack and Google Page Speed is 100 with this and 74 without it!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.