Approach for sniffing Firefox 4 and higher

239 views Asked by At

I am writing a JavaScript library that must check for Firefox 4 or higher. Trust me, I need to.

I'm planning to go with the following sniffing code:

if ('MozAppearance' in document.documentElement.style) {

    //We have Mozilla

    if (!!window.FormData) {

        //We have Firefox4+

    }
}

I feel ok about it because it seems rather unlikely (to me) that anyone will add a global FormData method.

Assuming I'm comfortable with the usual risks associated with any browser sniffing method, can you see any problems with this?

Documentation for window.FormData in Firefox is here:

https://developer.mozilla.org/en/DOM/XMLHttpRequest/FormData

Thanks!

3

There are 3 answers

1
vcsjones On

That's up to you to decide if that is reasonable feature detection for you. If you believe that testing for the presence of FormData is enough; then go with that.

You can "increase" the features you check for as well. You can also look for window.URL, a new feature in Firefox 4 as well:

if (!!window.FormData && !!window.URL) {

    //We have Firefox4+

}

So now your odds that someone declared both are even lower. You could also get the user-agent involved as well if you trust your users enough to not change them.

0
Louis Ricci On

window.history.pushState is another one you can check. I think we could do this all day with features that appear in FF4+ but not FF3.x.

1
Knu On

Assuming I'm comfortable with the usual risks associated with any browser sniffing method, can you see any problems with this?

It will break the moment they drop the prefix.
All the -moz- properties tagged obsolete attest of that fact.
So remember you will have to continually add recent Mozilla extensions using ||.