33

我们有一个网站,其中列出了很多活动,并希望为每个活动添加讨论。

所以我们想使用disqus,并检查了它。原来他们使用全局变量来配置实例。

像;

var disqus_shortname = '';

var disqus_identifier = '';

var disqus_url = '';

这给我们带来了一个问题,当我们不想使用相同的标识符,而是每个 disqus 实例使用一个唯一的标识符时。尝试将每个实例化 + 配置放在 iframe 中,但这确实搞砸了 ie8。有更好的方法吗?

所以,总结一下;一页上有几个disqus实例。如何?有人做过吗?

谢谢

4

8 回答 8

24

我们遇到了类似的问题,并就此向 Disqus 发送了电子邮件。他们确认默认情况下每页只支持一个 Disqus 模块。

在浏览 Disqus JS 文档时,我确实找到了一个可能对您有用的解决方案,方法是在用户与网站交互时加载和卸载 Disqus 模块:

DISQUS.reset({
  reload: true,
  config: function () {  
    this.page.identifier = "newidentifier";  
    this.page.url = "http://example.com/#!newthread";
  }
});

http://docs.disqus.com/help/85/

确切的实施将取决于您的站点,但这应该为您提供一个构建块开始。例如,如果事件信息通过扩展内容区域变得可用,则您可以在有人扩展事件内容时加载 Disqus 模块。

于 2011-07-22T21:10:25.097 回答
17

我写了一篇关于这个的文章,在这里找到它。http://mystrd.at/articles/multiple-disqus-threads-on-one-page/

本质上,如果您可以一次显示一个模块并使用某种“显示评论”控件,您可以通过以下方式进行操作(以 Wordpress 和 jQuery 为例,但您可以调整内容标识符基于您的需要)。在后循环中,为每个插入一个额外的控件:

<a onclick="loadDisqus(jQuery(this), '<?= $id ?> <?= $post->guid ?>', '<? the_permalink() ?>');">
   Show comments
</a>

之后,您需要一个通用函数来使用这些 post 参数并按需重新加载 Disqus。请注意,2012 版的 Disqus 还没有 reset() 方法,因此这将无法使用。

// global vars used by disqus
var disqus_shortname = 'yoursiteshortnameindisqus';
var disqus_identifier; // made of post id &nbsp; guid
var disqus_url; // post permalink

function loadDisqus(source, identifier, url) {
    if (window.DISQUS) {
        jQuery('#disqus_thread').appendTo(source.parent()); // append the HTML to the control parent

        // if Disqus exists, call it's reset method with new parameters
        DISQUS.reset({
            reload: true,
            config: function() {
                this.page.identifier = identifier;
                this.page.url = url;
            }
        });
    } else {
        //insert a wrapper in HTML after the relevant "show comments" link
        jQuery('<div id="disqus_thread"></div>').insertAfter(source);
        disqus_identifier = identifier; // set the identifier argument
        disqus_url = url; // set the permalink argument

        // append the Disqus embed script to HTML
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
        jQuery('head').appendChild(dsq);
    }
};

除此之外,我相信您将不得不求助于使用 iframe。此处概述了以 Ruby 为例的此类解决方案 - http://blog.devinterface.com/2012/01/how-to-insert-more-disqus-box-in-single-page/

于 2012-07-11T12:05:20.470 回答
3

据推测,截至 2012 年 7 月 17 日,Disqus 2012 现在再次支持“重置”。

于 2012-08-16T21:26:08.650 回答
1

我需要从 GWT 应用程序中使用 Disqus,因此我需要解决应用程序中的虚拟页面更改时按需加载线程的问题。

少量的逆向工程和实验使我构建了一个实用程序类(如下)。

主要见解是:

  1. 有一个未记录的全局参数disqus_container_id ,它允许您将评论放在您喜欢的任何地方。如果这在将来的某个版本中不起作用,我的后备方案是临时将目标元素的 id 设置为disqus_thread,添加注释,然后将其更改回原始 id。
  2. 由于这是使用 JSNI 为 GWT 开发的,因此我需要在原始窗口上下文中设置全局参数,可以通过$wnd. 我相应地更改了默认的 Disqus 嵌入代码。之前我没有意识到所有全局变量都在 Window 对象中,但是我学到了一些新东西。
  3. 您可以重复使用同一个容器,Disqus 似乎在您激活它时会清除内容。
  4. 这会在 DOM 中留下大量 script 标签的副本。也许在它们被使用后清理它们也是一个好主意。DISQUS.reset或者,我可能会使用其他答案中描述的方法进行更多实验。

只为单独使用 JS 的人提取关键信息,这应该允许您将 Disqus 线程粘贴到您喜欢的任何地方:

function loadComments(container_id, shortname, identifier, developer) {
    // CONFIGURATION VARIABLES
    window.disqus_container_id = container_id;
    window.disqus_developer = developer ? 1 : 0;
    window.disqus_shortname = shortname; // required
    if (identifier) window.disqus_identifier = identifier;

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

这是完整的 GWT 实用程序类。到目前为止,我只实现了我需要的参数。

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.Widget;

public class Disqus {

    public static boolean developer = false;
    public static String shortname;

    public static void showComments(Widget where, String identifier) {
        showComments(where.getElement(), identifier);
    }

    public static void showComments(Element where, String identifier) {
        if (shortname == null)
            throw new IllegalArgumentException(
                      "You must specify the disqus shortname before displaying comments");

        // Store the original id of the target element
        String id = where.getId();
        if (id == null) {
            id = "disqus-" + Integer.toHexString(Random.nextInt());
            where.setId(id);
        }

        // Update the id temporarily
        where.setId("disqus_thread");

        // Load the comments
        loadComments(id, shortname, identifier, developer);
    }

    private static native void loadComments(String container_id, String shortname, String identifier, boolean developer) /*-{
        // CONFIGURATION VARIABLES
        $wnd.disqus_container_id = container_id;
        $wnd.disqus_developer = developer ? 1 : 0;
        $wnd.disqus_shortname = shortname; // required
        if (identifier) $wnd.disqus_identifier = identifier;

        // TODO
        // disqus_url

        // disqus_title

        // disqus_category_id

        // DON'T EDIT BELOW THIS LINE (sorry, I've edited it anyway)
        (function() {
            var dsq = $doc.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'http://' + shortname + '.disqus.com/embed.js';
            ($doc.getElementsByTagName('head')[0] || $doc.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    }-*/;
}
于 2012-08-28T22:58:02.297 回答
1

滕特伊索:

<div class="showDisqus" data-title="MyTitle" data-id="1" data-url="mysite.com/mypost">Show Comments</div>

$('.showDisqus').on('click', function(){   // click event of the show comments button
    var this_ = $(this);
        disqus_shortname = 'your_shortname',
        title = $(this).attr('data-title'),
        identifier = parseFloat($(this).attr('data-id')),
        url = $(this).attr('data-url');

    if (window.DISQUS) {

        DISQUS.reset({ // Remove the old call
          reload: false,
          config: function () {
          this.page.identifier = window.old_identifier;
          this.page.url = window.old_url;
          this.page.title = window.old_title;
          }
        });
        $('.showDisqus').show();
        $('#disqus_thread').remove();

        $('<div id="disqus_thread"></div>').insertAfter(this_);

        setTimeout( function() { // Creates a new call DISQUS, with the new ID
            DISQUS.reset({
              reload: true,
              config: function () {
              this.page.identifier = identifier;
              this.page.url = url;
              this.page.title = title;
              }
            });
            window.old_identifier = identifier;
            window.old_url = url;
            window.old_title = title;
        });

    } else {

        var disqus_identifier = parseFloat(identifier),
            disqus_title = title,
            disqus_url = url;

        $('<div id="disqus_thread"></div>').insertAfter(this_);

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

        setTimeout( function() { // Sorry, there must be a better way to force the ID called correctly
            DISQUS.reset({
              reload: true,
              config: function () {
              this.page.identifier = identifier;
              this.page.url = url;
              this.page.title = title;
              }
            });
        },500);

        window.old_identifier = identifier;
        window.old_url = url;
        window.old_title = title;

    }
    $(this).fadeOut();  // remove the show comments button
});
于 2015-03-03T13:23:14.373 回答
1

我遍历了上面的解决方案,没有一个是开箱即用的。通过源代码检查,我拼凑了这个,它有效。它并不遥远,但看起来却有所不同。

<script type="text/javascript">
var disqus_shortname  = 'superchocolate',
    disqus_identifier = 'default',
    disqus_title      = 'I Heart Chocoloate',
    disqus_config     = function(){
        this.language = 'en';
    };


(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);
})();



function loadDisqus( identifier, url, title )
{
    DISQUS.reset({
        reload: true,
        config: function ()
        {
            this.page.identifier = identifier;
            this.page.url        = url;
            this.page.title      = title;
            this.language        = 'en';
        }
    });
}
</script>

在您的标记中,放置标准:

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

然后在您的点击操作中,这非常简单。我有一个名为“数据”的成员,我从 AJAX 调用中恢复过来。

loadDisqus( 'ugc-' + data.id,  location.protocol+'//'+location.hostname + "/ugc-submission-" + data.id + "/", data.title );

这对我有用,解决了上面代码中的一个问题,该问题在多个线程之间传递了类似的注释。

我在 Bootstrap 模式中显示我的 Disqus 线程,我在调用 $(el).moda('show') 之前调用 loadDisqus 并且它是无缝的。

于 2015-09-02T16:40:47.093 回答
0

我知道这个问题已经很老了,但是当我遇到同样的问题时,我发现了一个对我来说非常有用的解决方法。

我目前有一个页面 - 我们称之为专辑 - 列出了属于该专辑的一系列图像。

单击图像将弹出一个带有当前图像的灯箱和一个特殊的侧边栏,该侧边栏通过 ajax 获取当前图像信息,如标题、日期、作者、评论等。(非常类似于 facebook 图像查看器/侧边栏评论)

我希望用户能够在主相册页面上发表评论,也可以对他们在灯箱侧边栏中查看的特定图像发表评论。

多亏了一些属于灯箱的回调函数,只要打开灯箱就会运行一个回调函数,我曾经用它临时将主专辑页面中的 div 'disqus_thread' 重命名为其他名称。

每当您更改灯箱内的图像时,都会运行另一个回调 - 这使我可以重新加载有关包含新 disqus_thread div 和强制 disqus_reset 的 javascript 的图像的侧边栏信息。

另一个回调在灯箱关闭时运行,这允许我将专辑评论 div 重命名为 disqus_thread 并强制再次重置。

总而言之,主页包含专辑的评论,当您单击图像时,我将原始 div 重命名为其他内容。然后通过 AJAX 获取一些信息,其中包含一个新的 disqus_thread div。我使用 DISQUS.reset 并在灯箱上加载评论。当我关闭灯箱时,我将原始 div 重命名为 disqus_thread 并强制再次重置。

我希望它可以帮助某人!

于 2014-11-05T20:32:16.767 回答
-3

您可以通过 iframe 加载每个实例。不过,您可能必须有页内滚动条……哎呀。

于 2011-03-01T16:23:07.800 回答