douhong1703 2013-11-05 17:12
浏览 16
已采纳

我想在我的网站上添加一些检测功能; 这些好选择吗? [关闭]

As I am recoding my entire website, I need to choose a couple of methods to detect both web browsers and mobile devices. From the lists below, could you please recommend the better option?

Here are my choices;

Browser Detection

  • Use IE-specific comment arguments to target IE6 - IE8 CSS files; IE9+ and other browsers will use the same CSS file
  • Web browser user agent string matches via PHP (not entire user agent strings!)

Mobile Device Detection

  • CSS3 media queries, although this could be problematic for IE versions that don't support them, and also for detecting mobile devices with resolutions capable of displaying the full website (such as a 4.7" 1080p display, or a 7" 1080p display)
  • User agent string matches via PHP (again, not entire strings!)

Ideally, I'd like all mobile devices (smartphones and tablets) to display the mobile version of the website, and be given the option to view the desktop version via a user agent switch, which I know Chrome, Firefox and Opera on Android offer (at the very least).

Thank you.

  • 写回答

1条回答 默认 最新

  • dongshan1959 2013-11-05 17:29
    关注

    You should not use browser detection, but feature detection. For this, I highly recommend Modernizr.

    In regards to device detection, I use PHP Mobile Detect, it works fantastically. However, most of the time you can get away with merely doing screensize detection via media queries:

    @media screen and (max-width:759px){
        /* mobile CSS */
    }
    
    @media screen and (min-width:760px){
        /* browser CSS */
    }
    

    That is obviously oversimplified, you can set it to whatever sizes you want, and have multiple versions (tablet, giant screen, etc.). More information can be found here.

    That said, media queries are not supported with IE8 and below, so you need to do a Javascript-based detection. If you use Modernizr, you can just detect it, replicate the screen-size CSS in a separate file, and apply that CSS file dynamically by giving your CSS link an ID:

    <link rel="stylesheet" type="text/CSS" id="ApplyQueries" href="queries.css" />
    

    And then dynamically apply the right file by screensize:

    if(document.documentElement.clientWidth < 760){
        document.getElementById('ApplyQueries').href = 'mobile.css';
    } else {
        document.getElementById('ApplyQueries').href = 'browser.css';
    }
    

    Again this is over-simplified, but should give you an idea of where to start.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题