Javascript jQuery 浏览器检测?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4498057/
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-08-23 12:39:31  来源:igfitidea点击:

jQuery browser detection?

javascriptjqueryinternet-explorer

提问by Dancer

Is there a good way to detect if the user's browser is Internet Explorer using jQuery?

是否有一种使用 jQuery 检测用户浏览器是否为 Internet Explorer 的好方法?

I have an issue with PNG graphics using IE and want to swap them for GIF's only if the user is viewing the site with IE.

我对使用 IE 的 PNG 图形有问题,并且仅当用户使用 IE 查看站点时才想将它们交换为 GIF。

回答by Nick Craver

You canusing $.browser, yes, but it's a bad idea to use browser detection:

可以使用$.browser,是的,但使用浏览器检测是个坏主意:

if($.browser.msie) { /* IE */ }

A better option for instance would be $.supportwhich is featuredetection, like this:

例如,更好的选择$.support特征检测,如下所示:

if(!$.support.opacity) { /* IE 6-8 */ }

$.support.opacityis false in browsers that don't support opacityin styling (though IE 7-8 handle transparent PNGs file, so this still isn't ideal, depending on what you're after...personally I think you'd be giving IE 7/8 users a sub-optimal experience).

$.support.opacity在不支持opacity样式的浏览器中是假的(尽管 IE 7-8 处理透明的 PNG 文件,所以这仍然不理想,这取决于你的追求......我个人认为你会给 IE 7 /8 用户次优体验)。

What you should reallydo is target IE6 which doesn'tsupport transparent PNGs (without an alpha filter), like this:

真正应该做的是定位支持透明 PNG(没有 alpha 过滤器)的IE6 ,如下所示:

<!--[if IE 6]>
  <link rel="stylesheet" type="text/css" href="IE6ImageStyles.css">
<![endif]-->

回答by Spiny Norman

Yes, you can, but they prefer you to use jQuery.support: http://api.jquery.com/jQuery.support/.

是的,你可以,但他们更喜欢你使用jQuery.supporthttp: //api.jquery.com/jQuery.support/

In this case, use jQuery.support.opacity.

在这种情况下,请使用jQuery.support.opacity.

Edit: assuming this is about opacity, of course.

编辑:当然,假设这是关于不透明度的。

回答by MistereeDevlord

$.browser has been removed in 1.9as it's now suggested feature detection was preferred via $.support

$.browser 已在1.9 中删除,因为现在建议通过 $.support 首选功能检测

回答by numediaweb

For better browser detection jQuery strongly recommend the use of an external library such as Modernizrinstead of dependency on properties in jQuery.supportor the deprecated jQuery.browser(removed since v1.9)

为了更好地检测浏览器,jQuery 强烈建议使用外部库,例如Modernizr,而不是依赖于jQuery.support或已弃用的jQuery.browser(自 v1.9 后删除)中的属性

回答by CodeOverRide

$.browser.webkit
$.browser.safari
$.browser.opera
$.browser.msie
$.browser.mozilla

if ($.browser.msie) {
        //do something
}
else if ($.browser.mozilla) {
        //do something else
}

works with jQuery 1.3

适用于 jQuery 1.3

回答by Sarfraz

Check out the $.browserfunction.

查看$.browser功能。

To detect IE, you can also and better go for IE conditional comments.

要检测 IE,您还可以更好地使用IE 条件注释

Example:

例子:

<!--[if IE]>
  Special instructions for IE here
<![endif]-->

回答by oditiwebs.com

<script>
    jQuery.each(jQuery.browser, function(i, val) {
      $("<div>" + i + " : <span>" + val + "</span>")
                .appendTo(document.body);
    });</script>

$.browser.msie
for IE


IE 的$.browser.msie

回答by Thomas Clayson

I realise this isn't an answer - but I can't really post this in a comment!

我意识到这不是一个答案 - 但我真的不能在评论中发布这个!

If you're going to use javascript this code fixes the png issue with ie6. I haven't used it much, but afaik you need a transparent gif image called x.gif and it does the rest automatically.

如果您打算使用 javascript,此代码将修复 ie6 的 png 问题。我没有经常使用它,但是你需要一个名为 x.gif 的透明 gif 图像,它会自动完成其余的工作。

// JavaScript Document

var supersleight    = function() {

    var root = false;
    var applyPositioning = true;

    // Path to a transparent GIF image
    var shim            = 'x.gif';

    // RegExp to match above GIF image name
    var shim_pattern    = /x\.gif$/i;



    var fnLoadPngs = function() { 
        if (root) {
            root = document.getElementById(root);
        }else{
            root = document;
        }
        for (var i = root.all.length - 1, obj = null; (obj = root.all[i]); i--) {
            // background pngs
            if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) {
                bg_fnFixPng(obj);
            }
            // image elements
            if (obj.tagName=='IMG' && obj.src.match(/\.png$/i) !== null){
                el_fnFixPng(obj);
            }
            // apply position to 'active' elements
            if (applyPositioning && (obj.tagName=='A' || obj.tagName=='INPUT') && obj.style.position === ''){
                obj.style.position = 'relative';
            }
        }
    };

    var bg_fnFixPng = function(obj) {
        var mode = 'scale';
        var bg  = obj.currentStyle.backgroundImage;
        var src = bg.substring(5,bg.length-2);
        if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
            mode = 'crop';
        }
        obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
        obj.style.backgroundImage = 'url('+shim+')';
    };

    var el_fnFixPng = function(img) {
        var src = img.src;
        img.style.width = img.width + "px";
        img.style.height = img.height + "px";
        img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
        img.src = shim;
    };

    var addLoadEvent = function(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                if (oldonload) {
                    oldonload();
                }
                func();
            };
        }
    };

    return {
        init: function() { 
            addLoadEvent(fnLoadPngs);
        },

        limitTo: function(el) {
            root = el;
        },

        run: function() {
            fnLoadPngs();
        }
    };
}();

// limit to part of the page ... pass an ID to limitTo:
// supersleight.limitTo('header');

supersleight.init();