Detect phone vs tablet

后端 未结 9 825
别跟我提以往
别跟我提以往 2020-12-16 11:08

Is there a way to detect if the user is using a tablet or a phone? As an example a person surfing the web using a tablet (any android tablet with version 3+ and iPad) they

9条回答
  •  甜味超标
    2020-12-16 11:44

    I think you're making a fundamentally arbitrary distinction between tablet, phone, or any other web enabled device here. It seems like the physical dimensions of the screen is the metric you want to use to dictate the content you serve.

    In this case I would try to implement logic based on values passed by the user agent in the HTTP headers ([mobiforge.com...]) and degrade gracefully to prompting the user if information isn't available.

    Your logic might look a bit like this:

    • If the user agent supplies a physical screen size in HTTP headers
      physical dimensions = UA value.
    • otherwise, if the user agent supplies a resolution and pixel dimensions
      physical dimensions = pixel dimensions divided by resolution.
    • (optionally) otherwise, use client side script to detect resolution and PPI
    • otherwise, if the user agent string looks like some kind of mobile device (regex)
      prompt the user to select.
    • otherwise
      assume a default option.

    Update: My answer is now three years old. It's worth noting that support for responsive design has progressed significantly and its now common to deliver the same content to all devices and rely on css media queries to present the site in a way that is most effective for the device it is being viewed on.

提交回复
热议问题