【问题标题】:Same disqus thread is loading for all articles正在为所有文章加载相同的 disqus 线程
【发布时间】:2014-03-21 03:56:10
【问题描述】:

我有使用 durandaljs 和 knockoutjs 的 SPA 页面。现在我将为我的网站实现对 disqus 插件的支持。

当我添加 disqus 时,我发现相同的 cmets 出现在具有不同 URL 的不同页面(我使用 durandal 进行 URL 重写)。

我猜这个问题的根源是我重写的 URL 由完整的 hashbang(#!) 来区分

代码如下:

    var disqus_shortname = 'somename';
    var disqus_identifier = context.id;

    (function () {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    if (typeof DISQUS != 'undefined') {
        DISQUS.reset({
            reload: true,
            config: function () {
                this.page.title = "title" + context.id;
                this.page.identifier = context.id;
                this.page.url = "http://localhost:23054/#!ideas/details/" + context.id;
                debugger;
            }
        });
    }

查看:

<div id="disqus_thread"></div>

我希望有人使用带有 SPA 页面的 disqus cmets 并且知道如何帮助我解决这个问题。

【问题讨论】:

标签: javascript knockout.js durandal disqus


【解决方案1】:

这是我的解决方案:

    var id = context.id;
    disqus_shortname = 'somename',
    disqus_identifier = id,
    disqus_title = id,
    disqus_url = "http://someurl.com/#!" + id;
        if ($('head script[src="http://' + disqus_shortname + '.disqus.com/embed.js"]').length == 0) {
            (function () {
                var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
                (document.getElementsByTagName('head')[0]).appendChild(dsq);
            })();
        }
        if (typeof DISQUS != "undefined") {
            DISQUS.reset({
                reload: true,
                config: function () {
                    this.page.identifier = id;
                    this.page.url = "http://someurl.com/#!" + id;
                }
            });
        }

【讨论】:

    【解决方案2】:

    我认为问题可能在于您的标识符不够唯一。来自 Disqus 文档:

    当访问启用 Disqus 的页面时,Disqus 使用此标识符 确定要加载的适当评论线程。如果适当 找不到线程,创建一个新线程。

    我不确定context.id 是什么,但请确保它对于每个线程都是唯一的,并且对于 Disqus 的用途而言足够独特。

    【讨论】:

      猜你喜欢
      • 2013-07-17
      • 1970-01-01
      • 2017-03-25
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多