just learned JQuery, ran into problems...

just learned JQuery, ran into problems...

I have a SQL database that contains a stored procedure that'll output data to a XML file every 5 minutes. The data outputted is always changing, while the format always is same. 

I wanted a page that'd use AJAX to pull the data from the server, check for existing data that was already displayed on the page, if so, update the data. If the data no longer existed in the XML, the data would be removed from the page. If there was new data in the XML but wasn't displayed in the page, it'd be added to the page. Keep in mind the page is on a site that requires HTTP authentication. 

A friend recommended using JQuery to simplify the task. 

I wrote the code, and guess what? It works. More and less so anyway. It works in IE, but breaks in any other browser. I've been plugging away at it for the last few days trying to figure out why it won't work in any other browser, and am about at my wit's end.

Perhaps you guys can catch the problem that's been eluding me. If you can't catch it, no worries-- I can simply tell my users to just use IE to load the page. 

Without further ado, here's the code:

  1. function updateQueue(){
  2.     // Pull queue data from server, send to queue processor. Disable caching to avoid IE caching issue. 
  3. var filtertype = document.getElementById('filtertype').value; // Find out what filter we're using. usually 'all', meaning no filter
  4. dump ('Using filter type: ' + filtertype);
  5. dump ('Now in updateQueue function...');
  6. $.ajax({
  7. type: "POST",
  8. url: "xxxx" + filtertype,
  9. cache: false,
  10. username: "xxxx",
  11. password: "xxxx",
  12. success: function(data) {
  13. dump('Successfully pulled data from server'); 
  14. processQueue(data); 
  15. }
  16. //error: function(data) { 
  17. // alert('Dang, it broke. Error: ' + data); 
  18. //}
  19. });
  20. }

  21. function dump(dumpwhat){
  22. if(enabledebugger == true){
  23. document.getElementById('debugthis').value += dumpwhat + '\n';
  24. }
  25. }

  26. function processQueue(data){
  27. // Convert the XML into a javascript array
  28. var r = 0;
  29. var player = new Array();
  30. dump('Now in processQueue function...');
  31. dump('Converting XML into an array...');
  32. $(data).find('row').each(function() {
  33. var row = $(this);
  34. if(row.length > 0) {
  35. player[r] = {
  36. 'SessionID' : row.attr("SessionID"),
  37. 'Source' : row.attr("Source"),
  38. 'Region' : row.attr("Region"),
  39. 'QueuedTime' : row.attr("QueuedTime"),
  40. 'Desc' : row.attr("Desc"),
  41. 'Game' : row.attr("Game"),
  42. 'QueueType' : row.attr("QueueType"),
  43. 'Device' : row.attr("Device"),
  44. 'ANI' : row.attr("ANI")
  45. };
  46. dump(r + ': ' + player[r]['SessionID']);
  47. if(!player[r]['Source']) player[r]['Source'] = '';
  48. if(!player[r]['ANI']) player[r]['ANI'] = '';
  49. }
  50. r++;
  51. });
  52. dump('Done converting XML into an array.');
  53. //return;
  54. // Convert queue table into a javascript array
  55. dump('Converting HTML table into an array...');
  56. var myTable = [];
  57. $('#sortabletable tr').each(function(i, tr) {
  58. var myTr = [];

  59. $('td', tr).each(function(i, td) {
  60. myTr.push($(td).html());
  61. });

  62. myTable.push(myTr);
  63. });
  64. dump('Done converting HTML table into an array.');
  65. // Now check each row, get Session ID
  66. var SessionIDtoCheckFor=0;
  67. var i=0;
  68. var j=0;
  69. var uBoundOfMyTable = myTable.length - 1;
  70. var uBoundOfplayer = player.length;
  71. var SessionIDFound = false;
  72. var SessionIDIndex=0;
  73. dump('Upper limit of table array: ' + uBoundOfMyTable);
  74. dump('Upper limit of player (XML) array: ' + uBoundOfplayer);

  75. dump('Now at the section for processing already displayed sessions...');
  76. for (i=1;i<=uBoundOfMyTable;i++) { //start at 1, to skip table header row
  77. SessionIDtoCheckFor = myTable[i][0]; // Grab the session ID
  78. dump('Session to process: ' + SessionIDtoCheckFor);
  79. dump('Checking player array for session...');
  80. // Search XML array for session id
  81. for (j=0;j<uBoundOfplayer;j++) {
  82. if(uBoundOfplayer == 0 || j > uBoundOfplayer) break;
  83. if(player[j]['SessionID'] == SessionIDtoCheckFor){

  84. SessionIDFound = true; // Session ID has been found, exit loop.
  85. SessionIDIndex = j; // Store index location for later use
  86. dump('matching session found, it exists!');
  87. break;
  88. }
  89. }
  90. if(SessionIDFound){
  91. // Update QueuedTime (works!)
  92. $('#sortabletable tr').each(function() {
  93. var playerIdcellvalue = $(this).find("#SessionID").html();

  94. if(playerIdcellvalue == SessionIDtoCheckFor) {

  95. // Found the row with the cell
  96. var QueuedTimecell = $(this).find("#QueuedTime"); // get handle for the cell containing the corresponding cell
  97. $(QueuedTimecell).html(player[SessionIDIndex]['QueuedTime']); // Update cell with current queued time
  98. dump('player session in table updated with current Queued Time');
  99. }
  100. });
  101. } else {
  102. dump('Session was not found in player array, session expired.');
  103. dump('Searching for handle on expired session in HTML table...');
  104. // Delete row from queue table via fade out (works!)
  105. $('#sortabletable tr').each(function() {
  106. var playerIdcellvalue = $(this).find("#SessionID").text(); // get handle for the cell containing the corresponding id
  107. if(playerIdcellvalue == SessionIDtoCheckFor) {
  108. dump('Found handle!');
  109. //dump('found expired, deleting it...');
  110. // Found the row with the expired session
  111. $(this).fadeOut(500, function() { $(this).remove(); }); // Fade out the row, then remove it
  112. dump('Expired session ' + playerIdcellvalue + ' has been removed from table.');
  113. }
  114. });
  115. }
  116. // Move on to next session
  117. dump('Moving on to the next session...');
  118. }
  119. // Add new pending sessions (works in IE, but doesn't work in other browsers!)
  120. dump('Now at session add section...');
  121. if(player.length > 0) {
  122. dump('player.length > 0 confirmed...');
  123. dump('Now in for loop. ');
  124. for (j=0; j < player.length; j++) {
  125. SessionIDFound = false;
  126. dump('j value: ' + j + ' ... player length value: ' + player.length);
  127. SessionIDtoCheckFor = player[j]['SessionID'];
  128. dump('checking to see if session ' + SessionIDtoCheckFor + ' exists...');
  129. $('#sortabletable tr').each(function() {
  130. var playerIdcellvalue = $(this).find("#SessionID").text(); // get handle for the cell containing the corresponding id
  131. if(playerIdcellvalue == SessionIDtoCheckFor) {
  132. dump('Found session match: ' + SessionIDtoCheckFor);
  133. // Found the row with the cell
  134. SessionIDFound = true;
  135. }
  136. });
  137. if(SessionIDFound == false) {
  138. // New session!
  139. dump('No existing session found, adding session...');
  140. // Generate row...
  141. var a, b, c, d, e, f, g, h, i, newrow; //there's a better way to do this, but this way was done for debugging purposes

  142. a = "<tr class=''>";
  143. b = "<td id='SessionID'>" + player[j]['SessionID'] + "</td> ";
  144. c = "<td id='Device'>" + player[j]['Device'] + "</td> ";
  145. d = "<td id='Source'>" + player[j]['Source'] + player[j]['ANI'] + "</td> ";
  146. e = "<td id='Region'>" + player[j]['Region'] + "</td> ";
  147. f = "<td id='QueuedTime'>" + player[j]['QueuedTime'] + "</td> ";
  148. g = "<td id='Game'>" + player[j]['Game'] + "</td> ";
  149. h = "<td id='Desc'>" + player[j]['Desc'] + "</td></tr>";
  150. newrow = a + b + c + d + e + f + g + h;
  151. dump('row html to add: \n' + newrow);
  152. $('#sortabletable tr:last').after(newrow); // Add new row to the table
  153. dump('new session has been added to table.');

  154. // execute sort (on hold)
  155. }

  156. }
  157. }
  158. // TODO: Swap out sortable.js with this:
  159. // http://tablesorter.com/docs/

  160. }

  161. var enabledebugger = true;
  162. var productElement = document.getElementById('debugthis');
  163. if (productElement != null){
  164. enabledebugger = true;
  165. }
  166. setInterval( "updateQueue()", 4000 ); // Execute updateQueue function every four seconds. 
This is the html:

  1. <form id="filterform"><input type="hidden" id="filtertype" name="filtertype" value="<?=$QType;?>" /></form>
  2. <? if($enabledebugger){ ?><textarea id="debugthis" rows="30" cols="200">
  3. Debugger Console
  4. ------------------

  5. </textarea><? } ?>
  6. <table border="0" cellpadding="5" cellspacing="1" width="100%" class="sortable" id="sortabletable">
  7. <tbody>
  8. <tr class="header">
  9. <td class="header">Session ID</td>
  10. <td class="header">Device</td>
  11. <td class="header">Source:</td>
  12. <td class="header">State</td>
  13. <td class="header">Queued Time</td>
  14. <td class="header">Game</td>
  15. <td class="header">Device Info</td>
  16. </tr><?
  17. if($theorder = 'odd'){
  18. $theorder = 'even';
  19. } else {
  20. $theorder = 'odd';
  21. }?>
  22. </tbody>
  23. </table>
My apologies for wrong usage of JQuery/javascript; I'm still trying to wrap my head around it. Feel free to criticize it; I'm always appreciative for opportunities for improvement. My thanks in advance for your time, and feedback!