JSLint: "'HTMLElement' was used before it was defined."
if (element instanceof HTMLElement)
How do I fix this?
Do I have to add an exception or ignore it?
JSLint: "'HTMLElement' was used before it was defined."
if (element instanceof HTMLElement)
How do I fix this?
Do I have to add an exception or ignore it?
Share Improve this question edited Mar 1, 2013 at 0:34 ThinkingStiff 65.4k30 gold badges147 silver badges241 bronze badges asked Aug 20, 2011 at 13:44 XP1XP1 7,1938 gold badges59 silver badges63 bronze badges4 Answers
Reset to default 6Check "Tolerate misordered definitions".
This works for me if my entire script is:
var e;
if (e instanceof HTMLElement) {
alert("");
}
and the only checked box is "Tolerate misordered definitions".
The response I get is:
Global HTMLElement, alert, e
This checkbox only seems to apply to identifiers used in the global scope. If this is tried within a function body, JSLint will plain about alert
unless you check the box to "Assume console, alert". However the following trick does satisfy JSLint:
var HTMLElement = HTMLElement;
(function () {
var e;
if (e instanceof HTMLElement) {
alert("");
}
}());
This passes with checkboxes "Assume console, alert", "Tolerate misordered definitions", and "Tolerate missing use strict." I get the response:
Global HTMLElement
3 'anonymous'()
Variable e
Global HTMLElement
Complexity 2
Definitely a hack; /*global HTMLElement */
is best. Makes sense, though, after reading the JSLint instructions.
Looks like you'll have to add an exception for it. I could not find any of the checkbox options that remove the error.
You can also add HTMLElement to the predefined textbox at the bottom of the JSLint page (if you are using the online validation version).
Since I'm assuming you are in a browser this should be a valid exclusion.
change "alert" to "window.alert", and use the jslint directive /*jslint browser:true */ at the top.