Skip to content

Instantly share code, notes, and snippets.

@robwent
Last active September 9, 2021 07:59
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robwent/e9c321b5c200370da0cadb69e40379b7 to your computer and use it in GitHub Desktop.
Save robwent/e9c321b5c200370da0cadb69e40379b7 to your computer and use it in GitHub Desktop.
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>
Copy link

ghost commented Mar 5, 2018

Is it really Increase website loading speed?

@bershatsky
Copy link

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!!!

@kamauwanjoroge
Copy link

Do you have a similar tutorial for the default WP comments system using vanilla JS?

@robwent
Copy link
Author

robwent commented Oct 29, 2020

Sounds interesting but it's not something I've tried to do before.
Most of the sites I work on tend to have comments disabled.

@kamauwanjoroge
Copy link

kamauwanjoroge commented Oct 30, 2020

@robwent there is a similar plugin that does it, but it uses jQuery and using it increases my page loading time by nearly 2 seconds coz I don't use jQuery. I have tried to find a solution using vanilla JS to no avail. I (and many others) would really appreciate if you could kindly get something working for the default commenting system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment