How do you detect between a Desktop and Mobile Chrome User Agent?

前端 未结 1 562
半阙折子戏
半阙折子戏 2020-11-30 08:45

For a Chrome Desktop Extension home page, I\'m trying to detect whether a user is using Chrome for Desktop or Chrome for Mobile on Android. Currently the script below ident

相关标签:
1条回答
  • 2020-11-30 09:21

    The problem is the user agent will always have "Chrome" whether it is the desktop or mobile version. So you have to check the more specific case first.

    $(document).ready(function(){
        var ua = navigator.userAgent;
    
        if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua))
           $('a.mobile-other').show();
    
        else if(/Chrome/i.test(ua))
           $('a.chrome').show();
    
        else
           $('a.desktop-other').show();
    });
    
    0 讨论(0)
提交回复
热议问题