Skip to content

Instantly share code, notes, and snippets.

@abdul
Forked from robwent/disqus-ondemand.html
Created May 12, 2020 07:18
Show Gist options
  • Save abdul/dba4e3da639e889292bf259e8b053ca5 to your computer and use it in GitHub Desktop.
Save abdul/dba4e3da639e889292bf259e8b053ca5 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment