Plea to Mozilla

by KodefuGuru 18. August 2009 17:20

I had one of those problems that I had trouble resolving with a search engine this past weekend. Once it was pointed out to me what I did wrong, it was really quite simple. Basically, I needed the enter key to fire a function from a certain input textbox. I wrote up a jquery script, and it worked for IE, Chrome, and Safari, but it did not work for Firefox. Here was the script.

$("#Location").keydown(function(evt) {
    switch (event.keyCode) {
        case 13:
            findFresh();
            break;
    }
}); 

Do you see the problem? The parameter for the function is named evt, but I switched on event instead. When this was pointed out, I was perplexed that three different browsers would automagically map this parameter to a variable for me.

Then someone else told me the real reason this works in everything but Firefox: event refers to a nonstandard (if everything but Firefox is nonstandard) object known as window.event. This contains the information from the last event that was fired.

I can deal with that because I would prefer my functions to be as pure as possible, and analyzing a parameter is more pure than analyzing a global object. I changed my code to use the evt parameter and would do so even if Firefox supported window.event. However, I really do feel that Firefox goes out of its way to make a developer’s life miserable. It’s like they’re standing on the principle that if Microsoft initiated it, they want nothing to do with it.

Window.event is such a small item that one could let it slide, but what about when you need to do something with an element in JavaScript? In every other browser, you assign an ID to the element, then reference the ID directly as a variable. Not in Firefox. They force you to call document.getElementById, and this preserves no functional purity as you’re accessing the global variable document. There is no reasoning behind this other than asininity.

Message to the Mozilla team: there are hundreds of thousands of search hits related to people trying to figure out how to make their code work in Firefox. Their code works in all other major browsers, but they are wasting hours of their precious evenings and weekends pleasing users of your browser. Please consider the millions of wasted man-hours and make your browser compatible with IE, Chrome, and Safari.

Tags: , ,

Kodefu

Comments

8/18/2009 5:24:26 PM #

trackback

Trackback from WebDevVote.com

Plea to Mozilla

WebDevVote.com

8/18/2009 5:38:44 PM #

trackback

Trackback from DotNetKicks.com

Plea to Mozilla

DotNetKicks.com

8/18/2009 6:43:54 PM #

Jimmy Sawczuk

I'll probably get flamed for this, but I never really understood this either - how is it "following standards" if you're the only browser that supports it? For all those years, IE WAS the standard, and now all these others come along and say "no, false, you're not the standard, we are"? I know its supposed to help us developers, but it causes a ton of headaches for everyone involved if the rules are changed just for the sake of changing the rules. If it ain't broke, don't fix it, as it were.

Jimmy Sawczuk United States

8/19/2009 8:15:48 AM #

Tom

Or you could make it so that your textbox has a default button and hitting enter will fire the button.  This way users can hit enter, tab to the button and hit enter, or use the mouse.

Personally, I think having them hit enter on the textbox is a design flaw that should be avoided.

Tom United States

8/19/2009 9:23:33 AM #

chris

Hey Tom,

I admit I'm not an html guru. I like to think I write clean html (just like I know I write clean code), but I'm missing a lot of tricks from my toolbox. How do I set a default button on a textbox? They are both just input elements.

The behavior you describe is the behavior I have in my site: http://findfresh.us. I never required anyone to click enter in the textbox. The code was merely to support browsers other than IE. A friend of mine on twitter complained that hitting enter in the textbox didn't fire the search, and I discovered that without any javascript it was only working in IE.

chris United States

9/8/2009 9:45:53 AM #

Evan Larsen

I think Firefox passes the event to the function whereas IE will just assign the event to the global variable window.event. Also, keyCode is not used across all browsers, so try and check for charCode also.  Also, I liked your presentation at code camp.
Try this code instead:

$("#Location").keydown(function(evt) {
    evt = evt || window.event;
    var keyCode = evt.keyCode ? evt.keyCode : evt.charCode;
    switch (keyCode) {
        case 13:
            findFresh();
            break;
    }
});

Evan Larsen United States

9/8/2009 11:30:29 AM #

chris

Thanks for the feedback, Evan! Changing event to evt works in the browsers that I tested: IE, Firefox, Safari, and Chrome.

chris United States

9/17/2009 1:41:53 PM #

JohnM

If you're going to play with interpreting key event keystroke values, an invaluable resource to keep on hand for evaluating them in various browsers is http://www.w3.org/2002/09/tests/keys-cancel2.html. It has saved me countless headaches and hair pulling. It clearly shows you the difference between keyCode and charCode in various browsers. For example, in Firefox, keyCode is the hardware device key location code whereas charCode is the ANSI character value of the printable keystroke (e.g. charCode: a .vs. A, keyCode: duplexed 8/* key has same value), but in IE you only have the keyCode property and it's meaning depends on the event being processed.

JohnM United States

9/24/2009 10:48:49 PM #

Neb

Thanks that is helpful

Neb

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Whois KodefuGuru

Chris Eargle

Chris Eargle
.NET Community Champion

LinkedIn Twitter Technorati Facebook

MVP - Visual C#

 

INETA Community Champions
Friend of RedGate
Telerik .NET Ninja
Community blogs & blog posts

I am a #52er


World Map

RecentComments

Comment RSS

Tag cloud

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010