javascript 检测用户代理 Firefox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14843713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:44:03  来源:igfitidea点击:

Detecting User Agent Firefox

javascripthtmluser-agent

提问by user2062745

I'm pretty new to Javascript. What I've learned is from just playing around with code.

我对 Javascript 很陌生。我所学到的只是玩弄代码。

I'm having trouble getting this to load. Bascially I need a certain div to only show in Firefox. Here is what I have.

我无法加载它。基本上我需要一个特定的 div 才能只在 Firefox 中显示。这是我所拥有的。

<div id="parent" class="control-group"></div>

<script type="text/javascript">
        $(document).ready(function() {
        switch ( BrowserDetect.browser )
        {
        case 'Firefox':
            $("button[name='btn']").click(function() {
                $("#parent").html("<div></div>");
            });
        });
        break;
        }
</script>

回答by Samsquanch

You probably shouldn't be doing it like this, but if you are absolutely sure you only want to target Firefox:

您可能不应该这样做,但是如果您绝对确定只想针对 Firefox:

var browser = navigator.userAgent.toLowerCase();
if (browser.indexOf('firefox') > -1) {
    alert('Firefox');
}

fiddle

小提琴