Reputation: 780861
One of our advertisers has a landing page that contains the following Javascript function:
sfHover = function() {
if (!document.getElementsByTagName) return false;
var sfEls1 = document.getElementById("catmenu").getElementsByTagName("li");
for (var i=0; i<sfEls1.length; i++) {
sfEls1[i].onmouseover=function() {
this.className+=" sfhover1";
}
sfEls1[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" sfhover1\\b"), "");
}
}
var sfEls1 = document.getElementById("menu").getElementsByTagName("li");
for (var i=0; i<sfEls1.length; i++) {
sfEls1[i].onmouseover=function() {
this.className+=" sfhover";
}
sfEls1[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
}
}
}
When going to the page in IE7, it reports a syntax error on the if
line. As far as I can tell, it's perfectly fine syntax; IE8 doesn't complain, nor does Chrome. The only potential issue I can see is that the return false;
statement isn't in curly braces, but they're optional; I tried adding them, and it didn't resolve the error.
Is this a bug in IE7?
UPDATE:
I removed everything but this function definition from the page, to ensure that it's not related to one of the many other scripts that it loads. The error still occurred.
I removed everything but the if
line, the error went away. I added back the next line, the error returned. The interesting thing is that the second line gets an expected error: there's no catmenu
element on the page (even before I removed all the HTML), so it correctly complains that document.getElementById(...) is null or not an object
. I've asked our marketing person to let the advertiser know about this problem.
So could this logic error be causing IE7 to think there's a syntax error on the preceding line? I added a catmenu
element to the page, and the syntax error went away as well.
Then I added back the rest of the function. The second sfEls1
assignment also references a nonexistent ID. This caused it to report the preceding }
line as a syntax error! I think that confirms that this is a weird IE7 bug.
Upvotes: 0
Views: 383
Reputation: 780861
This seems to be an error reporting bug in IE7. When it reports the error:
'document.getElementById(...)' is null or not an object
because getElementById()
doesn't find the ID on the page, it also reports an extraneous syntax error on the line preceding this.
So there is an error in the function, just not the syntax error that this error message is complaining about. MS appears to have fixed this error reporting bug in IE8 -- Compatibility Mode doesn't even replicate the error.
Upvotes: 2