开发者

jQuery to add Browser-Name into css-class

I'm using jQuery to detect the Users Browser-Agent and than add the browser-name as a css-class to the body. This works like magic with webkit and mozilla … but with msie it fails — but don't know why. For msie i also have to add not only the «msie», but also one class with the version-number of the used msie (like «msie7» or «msie8»).

Everything works, only the msie-section does not!

my (full) jQuery-Code:

    <script type="text/javascript">
<!--
(function($){
    $(function(){

        var $copy = $('div.SPA_printcontact').clone();
        var $body开发者_开发百科 = $('body');

        $copy.insertAfter('#printdate');

        if($.browser.msie) {
            $body.addClass('msie')
            var userAgent = userAgent.substring(0,userAgent.indexOf('.'));
            var version = userAgent;
            $body.addClass('msie' + version)
        }
        if($.browser.webkit) {
            $body.addClass('webkit')
        }
        if($.browser.mozilla) {
            $body.addClass('mozilla')
        }

    });
}(jQuery));
-->
</script>


http://api.jquery.com/jQuery.browser/

Try using $.browser.version to get the version no instead

EDIT: This has been deprecated in jQuery v1.9 and upwards. Please refer to Tom's answer below.


There is no global object userAgent. It's a member of navigator, so use navigator.userAgent instead or the version provide by jQuery like suggested by Mohib Sheth


You can use Detectizr

https://github.com/barisaydinoglu/Detectizr

Detectizr is a Modernizr extension to detect

  • device
  • device model
  • device orientation
  • screen size
  • operating system
  • operating system version
  • operating system version full
  • browser
  • browser version
  • browser engine
  • browser plugins

Detection of these sets are optional and can be disabled.

alternatively there is also http://rafael.adm.br/css_browser_selector/ which Detectizr is based on (is more light weight)


Please try using $('body').addClass() if that does not work, then try $('body').attr("class",msie+version).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜