Hide an html element using javascript only if browser is firefox

前端 未结 6 1799
没有蜡笔的小新
没有蜡笔的小新 2021-01-05 07:28

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

6条回答
  •  一向
    一向 (楼主)
    2021-01-05 08:13

    “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 , before the body or target div are in the document, you could do something like:

    
    
    

提交回复
热议问题