Javascript selectedIndex NS_ERROR_FAILURE in Firefox
April 24th 2007
I was amazed that the top hit in Google for this was the Mozilla source code (which was helpful, but not very user-friendly) so I’m writing up a bit on this confusing error message.
This is a Mozilla (Firefox, Seamonkey, Ephiphany, etc.) exception that gets thrown when you try to set the selectedIndex of a select element out of bounds of its options. The error message (from Firebug) looks like:
uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMHTMLSelectElement.selectedIndex]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://www.example.com/test.html :: testSelect :: line 8" data: no]
This is not your standard JavaScript error. It’s not informative, useful, user-friendly, and barely gives you a line number to work with.
What it means, however, is very simple. The nsIDOMHTMLSelectElement component is an HTML Select Box, like so:
Now, let’s say you had a script triggered by a button that did something silly like set the selectedIndex of that select box to be an index that doesn’t exist, like 3:
function testSelect() {
// A bad selectedIndex:
document.testform.select1.selectedIndex = 3;
}
You’d think this would throw a nice Javascript error message, like “Javascript error: Index out of bounds.” But it doesn’t. It barfs out the garbage NS_ERROR_FAILURE exception from the browser widget itself because that index gets passed right on through.
Turn on Firebug and (sets the selectedIndex of the above to 3).
The conclusion: an out-of-bounds selectedIndex results in an NS_ERROR_FAILURE exception.
The solution: check that your selectedIndex is within the bounds of the option array! Never set it blindly without checking first.
Hopefully this saves someone the time looking through the Mozilla source for instances of the exception in the select widget C++ code… though that was kinda fun. If you see an NS_ERROR_FAILURE from a different widget or component, I can recommend searching the Mozilla code for the component it tells you about, it’s not too difficult. But for something as stupid as an index out of bounds, I hope this helps.








And it did. Thanks!
Thanks! Saved me a lot of time, this was very helpful
Yes, thank you very much for writing this article. This error confused me so much and I was delighted that you took the time to publish solutions for such an obscure and cryptic error.
Keep up the good work!
Thanks for this info—Google spitted only this site when I searched for this mysterious error. Good work!
No problem, glad you all found what you were looking for
That’s why I wrote it!
I’m no programmer by any means, and since this is my only result on google, this is the only place I can think of to say anything.
I get this error on starting up Firefox. It isn’t for the selected index thing, but rather for some nsiStringbundle.FormatStringFromName thing.
I have no idea what could be causing it, and it is driving me crazy.
Any ideas would be amazing since you seem to know what you are talking about.
Hi Travis, this isn’t the best place to ask about firefox problems. I’d post on the firefox message boards: http://forums.mozillazine.org/viewforum.php?f=38
What I would do first is change your start page (to see if it’s the page’s problem), then if you still get the error, go to Tools->Add-ons and disable add-ons one at a time until you find the culprit.
If that still doesn’t work, re-install firefox. Just download the latest version from http://www.mozilla.com and run the installer. It won’t hurt anything to do that.
Good luck!
hi,
am grateful to you.
yes ur the one.
may i know the good forums on javascript.
mostly i want the forum where you usually glance
with regards
himagiri
hi all
can i have good forum concerns java
thanking you all
Sorry, I do not know any Javascript forums. What I learned I learned myself, not by letting others do things for me.
Dear tristan ,
i think, through forums we come across different types of realtime problems &soluions in variety ways as it was from open forum,not the intension to share my work with others. In gain of theoritical knowledge(pdf’s) concerns there are many authors rests on web with lots&lot of stuff but not in a helpfulmode.
That’s why people used to visit forums where to share either knowledge or ideas from a realtime authors hopes like u.
problem arises only when reality goes far from practicality.
first information has to be borrowed from outside.
it doesn’t mean copying ideas or making oters to work
for us..
Thanks for the reply.
I agree with you completely, thank you for your thoughtful reply. Thanks for asking me, I’ll take it as a compliment, and I am sorry for the initial brash reply.
Personally I really like the Javascript libraries out there.
jQuery is my favorite, followed by mootools (though their community is arrogant and not as pleasant as jQuery), and then Prototype/Scriptaculous. Any of those are good places to start, as the library will do a lot of work for you and still be very efficient and easy to use.
Good luck!
Thanks .. this info is very useful
i’m having the same problem, but unable to fix it. I tried to set the selected index to the length of the select element, like this: document.formname.business.selectedIndex=document.formname.business.length;
ok. if I set selectedindex = length-1, it works.
Yep, that makes sense—since the index is zero-based, a list with 1 item will have item [0] only, 2 will have [0, 1], 3 will have [0, 1, 2], etc. So it’s length-1.
Helped, thank you!
Nice info and very helpful. Thank you very much!
Gosh, 3 hours of searching with collegues just to finally find out the mistake was that simple….arf!
Thank you so much for the help, I was going to abandon and try another way…
Great! I took me 30 seconds to find your post, problem solved. The web needs people like you.
Awesome, glad I could be of help.
Most of the skill is in knowing what people search for, and I am glad I can help there