如何在Disqus加载后运行javascript

lor*_*o83 5 javascript disqus

我正在尝试使用Disqus api,我需要运行一些修改Disqus评论主题的javascript代码.

如何在Disqus线程加载后运行javascript代码?

小智 6

我遇到了类似的问题.我能够想出的唯一可行的解​​决方案是运行setInterval()以检查Disqus容器div的高度.

例:

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);
Run Code Online (Sandbox Code Playgroud)


小智 0

这是 Brandon Morse 针对新版本 Disqus 的修改代码,当 Disqus 加载注释时脚本会停止。

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);
Run Code Online (Sandbox Code Playgroud)