开发者

Hide an html element using javascript only if browser is firefox

开发者 https://www.devze.com 2022-12-22 21:47 出处:网络
How can I hide a div with javascript if the browser is firefox only开发者_StackOverflow社区?To check Firefox browser

How can I hide a div with javascript if the browser is firefox only开发者_StackOverflow社区?


To check Firefox browser

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

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


<!-- HTML-->
<div id="divId" />


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.


“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>


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


function  detectBrowser(){
  ....
}

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

if (detectBrowser() === "firefox"){
  hDiv.style.display = "none"
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号