Please stop using features by testing for browser

Today I had to debug some code that up until fairly recently was working 100%.

The issue was the code (some jQuery) was checking to see if the browser was Firefox and if it was it should use some Firefox specific code;

if ($.browser.mozilla) { e.mozSlice(a,b) }

Up until version 30 of Firefox this code would have worked, but mozSlice hasn't been needed for some time and was finally removed as of Firefox 30.

Now, there are a few ways around this, a really naive way would be to add in some version checking as well, so if the browser is Firefox and the version is < 30 use mozSlice, else use slice.

These type of checks are really redundant and in 6 months time you will revisit the code and wonder what was so special about version 30, instead you can make your code much more clear by testing instead for the feature itself.

if (undefined !== e.mozSlice) { e.mozSlice(a, b) }

This code says, if we have access to mozSlice, lets use it.

This means that first of all if Internet Explorer decided to implement the mozSlice function instead of Slice this would work, it also means that if Firefox decided to drop mozSlice (which they have) nothing would break.

Recent posts View all

Talk CSS

FrontEnders - Web Performance

Sharing some of my thoughts on a FrontEnders meetup on the topic of Web Performance

Talk

Tips for how to ask for time off to attend conferences

Asking for time off to attend a conference can be nerve-racking, these tips will get you closer to attending the conferences you're interested in.