/*
Declare all your variables in one place near the top of your current scope.
This will help you keep track of them a lot easier.
JavaScript uses something called "variable hoisting", which effectively
means that any vars you declare in your code, get hoisted up and declared
at the "top" of the current scope.
See [3], [4] and [5] for info about scope
*/
var i = 0,
obj = {},
resultsLength = 0,
errorImage = "",
tableMiddle = "", //the fix for this code was to instantiate tableMiddle as a String
tableTop = "",
errorTable = "";
/*
Note that the above is just a short way to define multiple variables,
you could also do it like this:
var a, b, c, d;
or like this:
var a;
var b;
var c;
*/
obj = jQuery.parseJSON(data);
if( obj.errorIsSet == 1 ) {
errorImage = '<img src="images/warning.gif" alt="Warning!" width="24" height="24" style="float:left; margin: -5px 10px 0px 0px; " />';
tableTop = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
//getting the length of the results array and caching it
resultsLength = obj.results.length;
//using a regular for loop to iterate over an array
for( i = 0; i < resultsLength; i++ ) {
tableMiddle += '<tr><td>'+errorImage+'</td><td>'+obj.results[i]+'</td></tr>';
/*
I've replaced alerts with console.log() as it doesn't interfere with the flow
of the program (and saves you having to click the dialog boxes ever time).
See Links [1] and [2]
*/
console.log( tableMiddle );
}
tableBottom = '</table>';
errorTable = tableTop+tableMiddle+tableBottom;
console.log( errorTable );
$("div.error").empty(this).append(errorTable);
$("div.error").show();
}
Bookmarks