« archives

April 2007
S M T W T F S
« Mar   May »
1234567
891011121314
15161718192021
22232425262728
2930  

recently

news from around the web

» view all

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.


This entry was posted on Tuesday, April 24th, 2007 at 12:59 pm and is filed under Javascript, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


21 Responses to “Javascript selectedIndex NS_ERROR_FAILURE in Firefox”



  1. Nicole Commented at 9:25 am on May 1st 2007

    And it did. Thanks!

  2. Andrew Commented at 6:23 am on August 3rd 2007

    Thanks! Saved me a lot of time, this was very helpful

  3. Anon Commented at 5:38 am on August 31st 2007

    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!

  4. Dimitar Panayotov Commented at 11:02 pm on September 4th 2007

    Thanks for this info—Google spitted only this site when I searched for this mysterious error. Good work!

  5. Tristan Commented at 11:07 pm on September 4th 2007

    No problem, glad you all found what you were looking for :) That’s why I wrote it!

  6. Travis Wilson Commented at 6:12 am on September 27th 2007

    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.

  7. Tristan Commented at 8:48 am on September 27th 2007

    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!

  8. k.himagirinath reddy Commented at 9:53 pm on October 22nd 2007

    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

  9. k.himagirinath reddy Commented at 1:07 am on October 23rd 2007

    hi all
    can i have good forum concerns java

    thanking you all

  10. Tristan Commented at 12:46 am on October 25th 2007

    Sorry, I do not know any Javascript forums. What I learned I learned myself, not by letting others do things for me. ;-)

  11. k.himagirinath reddy Commented at 11:56 pm on October 25th 2007

    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.

  12. Tristan Commented at 12:01 am on October 26th 2007

    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!

  13. HR Commented at 3:02 am on November 22nd 2007

    Thanks .. this info is very useful

  14. omer Commented at 12:36 am on December 10th 2007

    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;

  15. omer Commented at 12:39 am on December 10th 2007

    ok. if I set selectedindex = length-1, it works.

  16. Tristan Commented at 10:19 am on December 10th 2007

    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.

  17. bzuK Commented at 5:08 am on February 26th 2008

    Helped, thank you!

  18. ytbpom Commented at 3:54 am on March 13th 2008

    Nice info and very helpful. Thank you very much!

  19. Gunshin Commented at 5:34 am on May 29th 2008

    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…

  20. Max Commented at 12:31 am on August 15th 2008

    Great! I took me 30 seconds to find your post, problem solved. The web needs people like you.

  21. Tristan Commented at 11:06 pm on August 15th 2008

    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 ;)

Leave a Reply

Some XHTML allowed.