What is causing these javascript validation errors (meaning a global and already defined var)?
Can anyone explain to me why:
function doAjax() {
var xmlHttpReq = false;
try { // Firefox, Opera 8.0+ and Safari
xmlHttpReq = new XMLHttpRequest();
}
catch (e) { // Internet Explorer
try {
xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support AJAX. Please use an AJAX compatible browser.");
return false;
}
}
}
xmlHttpReq.open('GET', 'handler.php', true);
xmlHttpReq.onreadystatechange = function() {
if (xmlHttpReq.readyState == 4) {
var response = xmlHttpReq.responseText;
handleAjaxResponse(response);
}
};
xmlHttpReq.send(null);
return true;
}
causes the following validation errors:
Error:
Implied global: ActiveXObject 8, XMLHttpRequest 4, alert 15, handleAjaxResponse 24
Problem at line 10 character 16: 'e' is already defined.
catch (e) {
Problem at line 14 character 20: 'e' is already defined.
catch (e) {
using javascript validator JSlint.com
source to share
It would make more sense to use a framework like jQuery (especially if you seriously want to support older versions of IE (pre v6)), but I'm guessing there is a reason why you don't.
It would be better if: a) you didn't nest try-catch, and b) you defined a set of functions, namely one to get the Xhr object, the other is to use the Xhr object to make a generic ajax request and an external "doAjax" function. which makes the specific ajax call you want to make: -
function getXHR()
{
var result = null
if (window.XMLHttpRequest)
{
result = new XMLHttpRequest();
}
else
{
try { result = new ActiveXObject("MSXML2.XMLHTTP.3.0") }
catch (e) { }
if (result == null)
{
try { result = new ActiveXObject("Microsoft.XMLHTTP") }
catch (e) { }
}
}
return result;
}
function ajaxRequest(url, data, callBack)
{
var xmlHttpReq = getXHR();
if (xmlHttpReq)
{
xmlHttpReq.open(data != null ? 'GET' : 'POST', url, true);
xmlHttpReq.onreadystatechange = function()
{
if (xmlHttpReq.readyState == 4)
{
//what happens if status is not 200
callBack(xmlHttpReq.responseText);
}
};
xmlHttpReq.send(null);
return true;
}
else
{
return false;
}
}
function doAjax()
{
var result = ajaxRequest('handler.php', null, handleAjaxResponse);
if (!result) alert("Your browser does not support AJAX. Please use an AJAX compatible browser.");
return result;
}
A further clarification would be to force the callback to accept an XHR object rather than the main responseText. This will give you a lot of flexibility. If the callback function just wants text, it can use this function: -
function getTextFromXhr(xhr)
{
xhr.onreadystatechange = fnVoid;
if (xhr.status == 200)
{
return xhr.responseText;
}
else
{
throw {number: xhr.status,
description: xhr.statusText,
responseText: xhr.responseText
}
}
}
source to share
Regarding the first error , here is exerpt from the JSLint documentation :
Undefined Variables and Functions
JavaScript's biggest problem is its dependency on globals, especially implied globals. If a variable is not explicitly declared (usually with var) then JavaScript assumes that the variable was global. This can be misspelled mask names and other problems.
JSLint expects all variables and functions to be declared before they are used or called. This allows the discovery of implied globals. this is also good practice because it makes programs easier to read.
Sometimes a file depends on global variables and functions that are defined elsewhere. You can identify them with JSLint by including a comment in your file that lists the global functions and objects that the program depends on, but they are not. defined in your program or scriptfile.
The global declaration might look like this:
/*global getElementByAttribute, breakCycles, hanoi */
The Global Declaration starts with
/*global
. Note that there is no space before g. You can have as many/*global
comments as you like. They must appear before the use they specify.
Regarding your problem, chances are this section will help you fix the errors:
Some global values may be predefined for you. Select "Assume Browser" (Browser) (see Options below) to predefine the standard global properties provided by web browsers, such as Window and Document and Warning. Select the intended rhino (rhino) to pre-define the global properties provided by Rhino Environment. Select "Assume Yahoo" Widget (widget) to pre-define global properties provided by Yahoo! Widgets.
the second error is listed because you are reusing the variable " e
" for every exception, including nested ones. Rename the variables for each exception to avoid this.
source to share