title: Javascript callback function issue
body: <h2>Background</h2>
  
  <p>I'm writing an asynchronous comment system for my website, after reading plenty of tutorials on how to accomplish this I started building one from scratch.  The comments are pulled using a JSON request and displayed using Javascript (jQuery).  When the user adds a new comment it goes through the hoops and finally is sent via AJAX to the backend where it's added to the database.  In the success section of the AJAX request I had the script empty the comments, then repull the new list (including the new post) and redisplay them.</p>
  
  <h2>Problem</h2>
  
  <p>While that was all nice, since it's making the page much shorter, then much longer it messes up where the user is viewing the page.  I wanted to have it readjust the page back down to the end of the comment list (where the add comment form is).  It also re-enables the add button, which was disabled when the clicked it to prevent impatient people from spamming.</p>
  
  <pre><code>$('#commentList').empty();
  getComments('blog', $('input#blogId').val());
  window.location = "#addComment";
  $('#comAdd').removeAttr('disabled');
  </code></pre>
  
  <p>While this worked all well and good in theory, it seemed that the browser was getting ahead of itself and processing the window.location before the getComments function was done.  So I read a little more and googled it and it seemed people were saying (for similar problems) to use callback functions, so I came up with this:</p>
  
  <pre><code>$('#commentList').empty();
  getComments('blog', $('input#blogId').val(), function() {
      window.location = "#addComment";
      $('#comAdd').removeAttr('disabled');
  });
  </code></pre>
  
  <p>This generates no javascript errors according to FireFox, but nothing within the callback function is working, it's not re-enabling the button nor changing the window.location.</p>
  
  <p>Any ideas?  Better ways to go about it?  Do I have a glaring typo that I can't seem to see?</p>
  
  <p>Thanks!</p>
  
  <h2>Update</h2>
  
  <p>I was under the impression the callback functions were a standard thing you could use.</p>
  
  <pre><code>function getComments(type, id)
  {
  
      $.getJSON("/ajax/"+type+"/comments?jsoncallback=&amp;id="+id, function(data) {
          for (var x = 0; x &lt; data.length; x++)
          {
              var div = $("&lt;div&gt;").addClass("comment").appendTo("#commentList");
              var fieldset = $("&lt;fieldset&gt;");
              var legend = $("&lt;legend&gt;").addClass("commentHeader");
              if ( data[x].url == "" )
              {
                  legend.text((x+1) + ' - ' + data[x].name);
              }
              else
              {
                  $("&lt;a&gt;").attr({href: data[x].url}).text((x+1) + ' - ' + data[x].name).appendTo(legend);
              }
              legend.appendTo(fieldset);
              $("&lt;div&gt;").addClass("date").text(data[x].timestamp).appendTo(fieldset);
              $("&lt;p&gt;").addClass("comment").text(data[x].content).appendTo(fieldset);
              fieldset.appendTo(div);
          }
      });
  
  }
  </code></pre>
  
  <p>This is called on document ready.  Pulling all the comments and displaying them inside the #commentList div.  When the user submits his/her comment it performs an AJAX request to a PHP script that adds the new comment to the database, upon success of this I have this:</p>
  
  <pre><code>$('#commentList').empty();
  getComments('blog', $('input#blogId').val());
  window.location = "#addComment";
  $('#comAdd').removeAttr('disabled');
  </code></pre>
  
  <p>Deletes all the comments from the page.
  Uses JSON to request the comments again (including the users new one).
  Moves the page to the #addComment anchor, which is where their new comment would be displayed.
  Re-enables the add comment button.</p>
  
  <p>The problem is that the browser does the window.location line before the getComments function is done rendering all the comments, so as the page grows the user isn't looking anywhere near their new comment.</p>