views:

213

answers:

7

How can I hide a div with javascript if the browser is firefox only?

+1  A: 

Just check a FF-specific JavaScript property. E.g.

var FF = (document.getBoxObjectFor != null || window.mozInnerScreenX != null);

if (FF) {
    document.getElementById("divId").style.display = 'none';
}

This is called feature detection which is preferred above useragent detection. Even the jQuery $.browser API (of which you'd have used if ($.browser.mozilla) for) recommends to avoid useragent detection.

BalusC
@Downvoter: how is feature detection bad?
BalusC
@BalusC: SO really needs to implement the @Downvoter thing or at least force people to explain their downvotes. I got three anonymous downvotes yesterday, 2 on an answer that has 10 upvotes! Robbed me of my bronze badge lol. /end rant - I've evened it out for you anyway.
Andy E
Think it's because your example isn't good - you aren't feature detecting the issue that causes the problem, you are feature detecting an unrelated feature that you know will be correct for firefox.
Rich Bradshaw
`mozInnerScreenX` is also only available from Firefox 3.6 on.
bobince
you could always do this `var FF = window.mozInnerScreenX ? true : false;`. PS. I didn't downvote you.
The Elite Gentleman
this isn't working for me. i just put it in script tags in the <body>. is that right?
Cameron
@Cameron: It has to be placed _after_ the div, otherwise document.getElementById() won't find the div.
dbemerlin
@Cameron: or execute during page onload.
BalusC
@bobince: this was from top of head. I forgot to add the `getBoxObjectFor`. Fixed (also see the "feature detection" link).
BalusC
@BalusC: no insult intended, but if you read that article about feature detection well, you should have known that you shouldn't use feature detection just to do browser detection.
Marcel Korpel
A: 

Try this link: http://www.quirksmode.org/js/detect.html

David Andersson
This depends on the user agent string which is fully spoofable.
BalusC
People who spoof user agents have to live with the consequences IMHO.
dbemerlin
@dbemerlin: there exist many unbranded Firefox versions (that don't have "Firefox" in the UA string) due to licence regulations: http://en.wikipedia.org/wiki/Mozilla_Firefox#Trademark_and_logo
Marcel Korpel
@Marcel: very true.
David Andersson
A: 
function  detectBrowser(){
  ....
}

hDiv = .... //getElementById or etc..

if (detectBrowser() === "firefox"){
  hDiv.style.display = "none"
}
Falcon
Sorry but I don't understand how this helps at all.
Andy E
Haha - this made me laugh."How do I detect if the browser is Firefox?""Write a function called `detectBrowser`, naturally."
ehdv
He didn't ask how to detect if it was Firefox; he asked how to hide it.
Dean J
+1  A: 

To check Firefox browser

//Javascript
var FIREFOX = /Firefox/i.test(navigator.userAgent);

if (FIREFOX) {
  document.getElementById("divId").style.display="none";
}


<!-- HTML-->
<div id="divId" />
The Elite Gentleman
@Downvoter, since when is this wrong? This works well in Firefox.
The Elite Gentleman
I'm not the Downvoter, but it doesn't work well on my browser (which identifies itself as ‘Shiretoko’), or many other Firefox variants.
bobince
@Bobince...The asker asked in Firefox....obviously you're not using plain old firefox.
The Elite Gentleman
Well, it's a woolly question, yes, but Shiretoko is one way an official but unbranded Firefox 3.5 identifies itself. It's 100% the same as Firefox except with no fox on the icon.
bobince
Ok, then it's using the Mozilla engine, just like Opera does but that doesn't make Opera Firefox (unfortunately).
The Elite Gentleman
"Current users of Mozilla Firefox should not use Shiretoko Alpha 1."Upvote to "Opera isn't Firefox, but is also built on the same engine."
Dean J
Opera doesn't use the **Gecko** rendering engine, but Presto. It doesn't even identify itself with the infamous "Mozilla" label in the User-Agent string (see http://www.nczonline.net/blog/2010/01/12/history-of-the-user-agent-string/ for info about this). And Bobince is right, there exist many unbranded Firefox versions (that don't have "Firefox" in the UA string) due to licence regulations: http://en.wikipedia.org/wiki/Mozilla_Firefox#Trademark_and_logo
Marcel Korpel
@Marcel, Opera 9 can "clone" IE/Firefox by changing the user agent string. Even though it runs Presto, when rendering "cloning" firefox, it uses the Mozilla engine, it even renders any CSS with "-moz-xxxx" attributes.
The Elite Gentleman
Do you have a source for this? I just tested this using Opera 10.10 and although it identifies itself as "Mozilla/5.0 (X11; Linux i686; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0" when using "Mask as Firefox", it still uses Presto (not Gecko and certainly not something called the Mozilla engine) and doesn't process `-moz-` prefixed properties, at least not `-moz-border-radius`, nor does it support `window.onbeforeunload`. Border radius is only supported as of Opera 10.50: http://dev.opera.com/articles/view/css3-border-background-boxshadow/#border-radius
Marcel Korpel
@Marcel, correct, I just did a recheck, and "-moz" css doesn't render (Strange since I'm not using "border-radius" in any css styles i'm using but Opera 10.50 displays it). I'm too tired, so I'll be off to bed. :-) (it's 3am here).
The Elite Gentleman
+1  A: 

“Is the browser Firefox” is almost always the wrong question. Sure, you can start grovelling through the User-Agent string, but it's so often misleading that it's not worth touching except as a very very last resort.

It's also a woolly question, as there are many browsers that are not Firefox, but are based around the same code so are effectively the same. Is SeaMonkey Firefox? Is Flock Firefox? Is Fennec Firefox? Is Iceweasel Firefox? Is Firebird (or Phoenix!) Firefox? Is Minefield Firefox?

The better route is to determine exactly why you want to treat Firefox differently, and feature-sniff for that one thing. For example, if you want to circumvent a bug in Gecko, you could try to trigger that bug and detect the wrong response from script.

If that's not possible for some reason, a general way to sniff for the Gecko renderer would be to check for the existence of a Mozilla-only property. For example:

if ('MozBinding' in document.body.style) {
    document.getElementById('hellononfirefoxers').style.display= 'none';
}

edit: if you need to do the test in <head>, before the body or target div are in the document, you could do something like:

<style type="text/css">
    html.firefox #somediv { display: none }
</style>
<script type="text/javascript">
    if ('MozBinding' in document.documentElement.style) {
        document.documentElement.className= 'firefox';
    }
</script>
bobince
This example errors in IE.
BalusC
Devil's Advocate: who really cares about SeaMonkey, Flock, Fennec, Iceweasel, and Minefield combined? That's maybe a hundredth-of-one-percent of userbase of a typical app, and when you're using one of those and it doesn't work... you often learn to occasionally expect quirks.
Dean J
@Dean: there are many users of Linux distribution that come with unbranded Firefox versions, called Iceweasel, Minefield, etc.
Marcel Korpel
Oh, good lord. This makes me like Ubuntu more, at least.
Dean J
No error in IE for me. Naturally, `document.body` must exist before using this particular test, so you couldn't do it in the `<head>`, but then you couldn't anyway since the `<div>` in question would already have to be in the page.
bobince
A: 

You might try Rafeal Lima's CSS Browser Selector script. It adds a few classes to the HTML element for OS, browser, js support, etc. You can then use these classes as hooks for further CSS and/or JS. You might write a CSS (or jQuery) selector like html.gecko div.hide-firefox once the script has run.

Andy Ford
A: 

if(document.body.style.MozTransform!=undefined) firefox only

kennebec