I was having an issue with Disqus - my comments were showing on post pages, but the comment count was not. Turns out that this is a simple to fix error relating to JavaScript scope.
Scope issue
The developer of Octopress has kindly protected our window
object from being polluted with various unnecessary variables by wrapping the whole disqus include in an anonymous function.
However, the Disqus script actually injects another <script>
element into the <head>
(or <body>
) of the page. This injected script, like all other scripts on the page, inherits the global (window
) scope, and not the scope of this anonymous function. Thus it will not have access to the disqus variables such as disqus_shortname
defined within the anonymous function. Without these variables, disqus loads such domains as undefined.disqus.com
which is not very helpful!
Solution
The fix is simple - open up source/_includes/disqus.html
and move the beginning of the anonymous function ((function () {
) so that it does not contain the var disqus_*
variable definitions. We also need to insert a couple of semi-colons that have been accidentally omitted. Here’s the resulting file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
One more thing
If you want your links to be absolute, then you should set root
to be your root URL in _config.yml
- e.g. root: http://www.benjiegillam.com/
rather than root: /
. This may or may not be required for Disqus to function properly.
EDIT: @Octopress has confirmed that root must not contain the scheme and host.
Bonus
Now that I’ve implemented the above, my comments and comment counts successfully display on my benjiegillam.dev
site (hosted by Pow)!