6

I am trying to work with Disqus api and I need to run some javascript code that modify Disqus comments thread.

How to run the javascript code after Disqus thread has loaded?

qwerty
  • 227
  • 1
  • 2
  • 10

5 Answers5

4

I ran into a similar issue. The only working solution I was able to come up with was to run setInterval() to check the height of the Disqus container div.

Example:

var editable = true; // set a flag
setInterval(function() {
// Initially Disqus renders this div with the height of 0px prior to the comments being loaded. So run a check to see if the comments have been loaded yet.
  var disqusHeight = $('#dsq-2').height();
  if ( disqusHeight > 0 ) {
    if (editable) { // To make sure that the changes you want to make only happen once check to see if the flag has been changed, if not run the changes and update the flag.
      editable = false;
      // Your code here...
    }
  }
}, 100);
morsecodemedia
  • 317
  • 2
  • 8
  • 1
    Awesome. This is a good enough solution for me, however, you need to change the way you instantiate that setInterval, as it'll keep running even once the test has completed. Assign it to a variable and clear the interval once the test passes. – Scotty Jan 16 '15 at 19:55
4

Try this:

function disqus_config() {
  this.callbacks.onReady.push(function () {
    // your code
  });
}
Abram
  • 39,950
  • 26
  • 134
  • 184
0

Here is modifyed code of Brandon Morse for new version Disqus and script stops when Disqus is loaded comments.

var interval = setInterval(function() {
    var $ = jQuery;
    var disqusHeight = $('#disqus_thread').height();
    if ( disqusHeight > 52 ) {  // height 52px is header of Disqus, more than 52px means that disqus load comments
      // Your code
        clearInterval(interval); // after loaded comment we stop this script
    }
}, 100);
0

Use flag to avoid loop:

evento.add(window, "load", function () {
    var w = window,
    d = document,
    a = d.getElementById("disqus_thread") || "",
    disqus_shortname = a ? (a.dataset.shortname || "") : "",
    embed_js_src = ("https:" == w.location.protocol ? "https" : "http") + "://" + disqus_shortname + ".disqus.com/embed.js",
    g = ".grid",
    h = ".grid-item",
    k = ".grid-sizer",
    grid = d.querySelector(g) || "";
    function build_layout() {
        if (grid) {
            if (w.Packery) {
                var pckry = new Packery(grid, {
                        itemSelector : h,
                        gutter : 0
                    });
            } else if (w.Masonry) {
                var msnry = new Masonry(grid, {
                        itemSelector : h,
                        columnWidth : k
                    });
            }
        }
    }
    build_layout();
    if (a && disqus_shortname) {
        w.loadJS && loadJS(embed_js_src, function () {
            if (grid) {
                var f = !1;
                setInterval(function () {
                    var disqus_thread_height = a.clientHeight || a.offsetHeight || "";
                    if (108 < disqus_thread_height && !1 === f) {
                        /* alert(disqus_thread_height); */
                        build_layout();
                        f = !0;
                    }
                }, 100);
            }
        });
    }
});
0
  `<script> 
        var disqus_config = function () {
         this.callbacks.onReady = [function(data) {
            //your code here
          }];
         this.callbacks.afterRender= [function(data) {
            //your code here
          }];
         this.callbacks.beforeComment= [function(data) {
            //your code here
          }];
         this.callbacks.onInit= [function(data) {
            //your code here
          }];
         this.callbacks.onNewComment= [function(data) {
            //your code here
          }];
         this.callbacks.onPaginate= [function(data) {
            //your code here
          }];
         this.callbacks.preData= [function(data) {
            //your code here
          }];
         this.callbacks.preInit= [function(data) {
            //your code here
          }];
         this.callbacks.preReset= [function(data) {
            //your code here
          }];

        };
  </script>`
K.P
  • 66
  • 7