Fix for jQuery.Disqus.js

Rob Loach's picture
// $Id$

/**
 * Disqus jQuery plugin
 * Applies Disqus comments to a page using a simple jQuery call.
 * $.Disqus({domain: "myusername"});
 *
 * Copyright (c) 2008 Rob Loach (http://www.robloach.net)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */


/**
 * Disqus Global Variables
 */

var disqus_developer = 1;
var disqus_container_id = 'disqus_container_id';
var disqus_message = '';
var disqus_title = '';
var disqus_url = '';

/**
 * Apply Disqus comments to the page.
 *
 * Usage:
 * $(document).ready(function(){
 *   $.Disqus({
 *     domain: "", // REQUIRED -- Your Disqus username/domain
 *     fragment: "disqus_thread", // Optional -- The fragment to associate with number of comment links
 *     title: "", // Optional -- The title of the current thread
 *     url: top.location.href, // Optional -- The url of the current thread
 *     message: "", // Optional -- A description of the current thread
 *     developer: 0, // Optional -- Determines whether or not we're working in a development environment
 *     container_id: "disqus_thread" // The container ID for the thread
 *   });
 * );
 * <div id="disqus_thread"></div>
 * <a href="http://example.com/my/page#disqus_thread">Add a comment</a>
 *
 * This will create the Disqus comment thread, stick it into #disqus_thread, and
 * replace "Add a comment" with the number of comments in that thread.
 */

jQuery.Disqus = function(options) {
  // Load the settings with defaults
  var settings = jQuery.extend({
    fragment: "disqus_thread",
    domain: "",
    title: "",
    url: top.location.href,
    message: "",
    developer: 0,
    container_id: "disqus_thread"
  }, options);
  if (settings.domain != "") {
    // Set the global Disqus variables
    disqus_developer = settings.developer;
    disqus_container_id = settings.container_id;
    disqus_message = settings.message;
    disqus_title = settings.title;
    disqus_url = settings.url;
           
    // Construct the query that will be sent to change the number of comments in links  
    var query = '?';
    $("a").each(function(i, obj){
          if (obj.href.split("#")[1] == settings.fragment) {
                query += 'url' + i + '=' + encodeURIComponent(obj.href) + '&';
          }
    });
   
    // Call the Disqus script to get the number of replies per link
    $.getScript('http://disqus.com/forums/' + settings.domain + '/get_num_replies.js' + query);
   
    // Call the Disqus script to create the Disqus comment thread
    alert('Attempting to create the comment thread!');
    //$('#' + disqus_container_id).load('http://disqus.com/forums/' + settings.domain + '/embed.js');
    //$('#' + disqus_container_id).text('').append('<script type="text/javascript" src="http://disqus.com/forums/' + settings.domain + '/get_num_replies.js"/>');
    //$.getScript('http://disqus.com/forums/' + settings.domain + '/embed.js');
   
    $('#' + disqus_container_id).appendDom([{
      tagName  :'script',
      type : 'text/javascript',
      src : 'http://disqus.com/forums/' + settings.domain + '/embed.js'
    }]);

    alert('Done creating thread, ready for the white screen?!');
  }
}

 
/**
 * appendDom - Extremely flexible tool for dynamic dom creation.
 *   http://byron-adams.com/projects/jquery/appendDom
 *
 * Copyright (c) 2007 Byron Adams (http://byron-adams.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 */

jQuery.fn.appendDom = function(template) {
  return this.each(function() {
    for (element in template) {
      var el = (typeof(template[element].tagName) === 'string') ?
        document.createElement(template[element].tagName): document.createTextNode('');
      delete template[element].tagName;
      for (attrib in template[element]) {
        switch ( typeof(template[element][attrib]) ) {
          case 'string' :
            if ( typeof(el[attrib]) === 'string' ) {          
             el[attrib] = template[element][attrib];
            } else {
              el.setAttribute(attrib, template[element][attrib]);
            }
            break;
          case 'function':
            el[attrib] = template[element][attrib];
            break;
          case 'object' :
            if (attrib === 'childNodes')  {$(el).appendDom(template[element][attrib]);}
            break;
        }
      }
      this.appendChild(el);
    }
  });
};

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.