Firefox bug

It seems I've found a Firefox bug in official build v2.0.0.1. This is really something, because I've never found any before (which is a miracle again since FF is known to be full of bugs). Smile It's in the JavaScript engine and involves properties of anchor JS objects.

Make a page with an anchor tag that has no href attribute and add some JS code that tries to enumerate the hostname property of the anchor objects ...

<html>
<body>
<a href="http://www.somesite.somewhere/">anchor #1</a>
<br>
<a>anchor #2</a>
<script type="text/javascript">
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
  alert(i + ". anchor, hostname=" + anchors[i].hostname);
}
</script>
</body>
</html>

It will display the hostname property of the first anchor object properly, but it'll throw the following exception for the second one:
Error: uncaught exception: [Exception... "Component returned failure code: 0x804b000a [nsIDOMNSHTMLAnchorElement.hostname]"  nsresult: "0x804b000a (<unknown>)"  location: "JS frame :: http://yourserver.yourdomain/example.html :: <TOP_LEVEL> :: line 9"  data: no]

The correct behaviour would be to return "undefined" for the hostname property of the second anchor. The exception is raised even if we try to check for the existence of the hostname property through a call to typeof, eg. if (typeof anchors[i].hostname != "undefined") { ... }

The bug is present in both the Windows and the Mac OS X build.
I've attached the example HTML file so you can test whether your version has the bug too. The exception is visible in the error console (available in the Tools menu).

AttachmentSize
firefox_2_0_0_1_anchor-JS_bug.html333 bytes

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

it's in 1.5 too, but there's a fix

Check for the href.value attribute instead.

if (!typeof anchors[i].href.value) { ... }

Hope this helps.....

Re: it's in 1.5 too, but there's a fix

Thanks for the workaround. It's actually the href attribute of the anchors (not href.value) that one should look for. However the hostname attribute is there to provide some convenience to the developer. Of course one can use the href attribute and dig out the hostname by himself (eg. with a regular expression), if one absolutely has to.

One strange thing I've found is that looking at my example page with Firebug I can see all the attributes of the anchor object and Firebug does not complain about any problem with the second anchor (that has no href). Or maybe Firebug just silently swallows the error thrown by Firefox, when it tries to evaluate all the attributes of the anchor object? Shock

PS: btw. Firefox 2.0.0.12 (Linux version) still has the bug.

still there in 2.0.0.17

The bug is still present in 2.0.0.17, but only under certain circumstances. For me it was triggered by having 2 lines of JS inline (just to start a script). Taking them away cures the problem

There is definitely no problem with my code & no other browser complains.

Just in case somebody else trips over this!

bug is fixed in Firefox 3.0.3

Maybe it was already fixed in 3.0.0, I didn't test it back then.