invoke/browser

一个用于检测浏览器和操作系统的小型类

1.0.0 2016-11-18 00:11 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:43:59 UTC


README

一个非常简单的类,它接受用户代理字符串并返回浏览器名称(Firefox,Chrome,Opera等)以及操作系统(Mac,Windows,Android等)。

原因

首先,你不应该使用浏览器嗅探来检测功能,使用Modernizr

我们主要用它来调整不同浏览器之间的样式。或者有时你可能需要添加一点脚本(按键处理差异可能难以检测特征)或调整特定平台组合,这时你可能需要检查这样做是否合理。

结果是相当粗糙的。我们仅在找到任何版本的IE时返回internet-explorer。没有版本信息。

对于平台,我们仅返回该操作系统。这意味着所有iOS设备(iPad,iPhone,iPod)都将返回ios

如果你需要了解设备的具体信息,使用媒体查询

支持

平台

  • Linux
  • iOS
  • Mac
  • Windows
  • Android
  • Blackberry

浏览器

  • MSIE
  • UC浏览器
  • Trident
  • Vivaldi
  • Firefox
  • Chrome
  • Opera
  • Opera Mini
  • Safari

用法

方法

getBrowser() // returns the string for the browser
getPlatform() // returns the platform for the browser
isMobile() // returns true for android and ios platforms (except iPad)
isDesktop() // returns the opposite of isMobile()

属性

<?php

$browser = new Browser($_SERVER['HTTP_USER_AGENT']);
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Browser Class</title>
  <style>
    body.google-chrome.mac {
      background: red;
    }
  </style>
</head>
<body class="<?php echo $browser->browser . ' ' . $browser->platform ?>">
  <!-- content -->
</body>
</html>

Macbook上Chrome的结果

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Browser Class</title>
  <style>
    /* the body would now be red */
    body.google-chrome.mac {
      background: red;
    }
  </style>
</head>
<body class="google-chrome mac">
  <!-- content -->
</body>
</html>