Forums Forums Search & Filter Pro Reloading Disqus comments counter with ajax enabled

Tagged: 

Viewing 7 posts - 1 through 7 (of 7 total)
  • Anonymous
    #202953

    Hello, i use a disqus JS to display comments count of the posts.
    Explained here: https://help.disqus.com/developer/adding-comment-count-links-to-your-home-page

    Basically all i do is add this javascript on the footer:
    <script id="dsq-count-scr" src="//EXAMPLE.disqus.com/count.js" async></script>

    What i’d need is to “reload” when results are updated. I read this: https://searchandfilter.com/documentation/faq/#will-my-lightbox-or-other-fancy-javascript-work-with-this-plugin

    But since i don’t know any javascript i don’t know how to put it together.

    Could you show me how it’s done please? Thanks.

    Trevor
    #202974

    Within that script that you load in the footer is likely to be a function that actually adds the comments. That you need to find (or ask them). The script you need to load that inside is this (replace the comment line with whatever function you need):

    <script>(function ( $ ) {
      "use strict";
      $(document).on("sf:ajaxfinish", ".searchandfilter", function(){
    // so load your scripts/function here again
      });
    }(jQuery));</script>
    Anonymous
    #202991

    The script is just 1 function. I added it under the comment but had no effect. I probably can’t copy it over i need to call the external script from their server i think. But how to call an external js from inside js?

    This is the real link: https://iso-2.disqus.com/count.js
    I used https://beautifier.io/ to make it more readable:

    var DISQUSWIDGETS, disqus_domain, disqus_shortname;
    typeof DISQUSWIDGETS === "undefined" && (DISQUSWIDGETS = function() {
        var f = document,
            a = f.getElementById("dsq-count-scr"),
            a = a && a.src.match(/(https?:)?\/\/(?:www\.)?([\w_\-]+)\.((?:dev\.)?disqus\.(?:com|org)(?::\d+)?)/i),
            e = {},
            s = f.head || f.body,
            j = {},
            q = {
                identifier: 1,
                url: 2
            };
        e.domain = a && a[3] || disqus_domain || "disqus.com";
        e.forum = a && a[2] || disqus_shortname;
        e.proto = a && a[1] || "";
        e.getCount = function(b) {
            var c;
            c = encodeURIComponent;
            var a = e.proto + "//" + e.forum + "." + e.domain + "/count-data.js?",
                d = [],
                k = 0,
                l = 10,
                r = "",
                b = b || {};
            b.reset &&
                (j = {}, r = "&_=" + +new Date);
            for (var b = [f.getElementsByTagName("A"), f.getElementsByClassName && f.getElementsByClassName("disqus-comment-count") || []], m, i, g, h, n = 0; n < b.length; n++) {
                m = b[n];
                for (var o = 0; o < m.length; o++) {
                    i = m[o];
                    g = i.getAttribute("data-disqus-identifier");
                    h = i.hash === "#disqus_thread" && i.href.replace("#disqus_thread", "") || i.getAttribute("data-disqus-url");
                    if (g) h = q.identifier;
                    else if (h) g = h, h = q.url;
                    else continue;
                    var p;
                    j.hasOwnProperty(g) ? p = j[g] : (p = j[g] = {
                        elements: [],
                        type: h
                    }, d.push(c(h) + "=" + c(g)));
                    p.elements.push(i)
                }
            }
            d.sort();
            for (c = d.slice(k, l); c.length;) b = f.createElement("script"), b.src = a + c.join("&") + r, s.appendChild(b), k += 10, l += 10, c = d.slice(k, l)
        };
        e.displayCount = function(b) {
            for (var c, a, d, e = b.counts, b = b.text.comments; c = e.shift();)
                if (a = j[c.id]) {
                    switch (c.comments) {
                        case 0:
                            d = b.zero;
                            break;
                        case 1:
                            d = b.one;
                            break;
                        default:
                            d = b.multiple
                    }
                    c = d.replace("{num}", c.comments);
                    a = a.elements;
                    for (d = a.length - 1; d >= 0; d--) a[d].innerHTML = c
                }
        };
        return e
    }());
    DISQUSWIDGETS.getCount();
    Anonymous
    #202993

    I couldn’t find a way to edit my above comment sorry!

    WORKING! i don’t have to redefine it, just use it:

    <script>(function ( $ ) {
      "use strict";
      $(document).on("sf:ajaxfinish", ".searchandfilter", function(){
    // so load your scripts/function here again
    DISQUSWIDGETS.getCount();
      });
    }(jQuery));</script>
    Anonymous
    #202995

    Sorry to triple post. It works on about 50% of the posts listed.
    Also if filter by a taxonomy it works (50-50) and then click “All taxonomies” (to show all posts again) or if i click the “Reset” button then none of the posts display the count.

    Is there any way to make the function bulletproof to trigger every time the content changes?

    Anonymous
    #203001

    I know i said i wouldn’t post again but this a definite solution, problem was with disqus:
    it’s: DISQUSWIDGETS.getCount({reset:true});
    not: DISQUSWIDGETS.getCount();
    Solution found here:
    https://stackoverflow.com/questions/37064242/disquss-count-js-script-doesnt-run-properly-in-index-html-with-react-js-websit

    Sorry for all spam thanks!

    Trevor
    #203012

    To quote the A Team, I love it when a plan comes together. Great to see you got it resolved, and thanks for posting the solution here, as others will be able to see it. I will close this thread for now.

Viewing 7 posts - 1 through 7 (of 7 total)