浏览器检测

发布于 2024-07-04 17:22:58 字数 174 浏览 11 评论 0原文

检测用户浏览器的最佳/最简单/最准确的方法是什么?

易于扩展和实施是一个优点。

使用的技术越少越好。

该解决方案可以是服务器端、客户端或两者。 不过,结果最终应该到达服务器。

该解决方案可以与框架无关。

该解决方案仅用于报告目的。

What's the best / simplest / most accurate way to detect the browser of a user?

Ease of extendability and implementation is a plus.

The less technologies used, the better.

The solution can be server side, client side, or both. The results should eventually end up at the server, though.

The solution can be framework agnostic.

The solution will only be used for reporting purposes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

白衬杉格子梦 2024-07-11 17:22:58

我最初问这个问题是因为我希望能够记录人们用来访问我的网站的浏览器和操作系统。 是的,用户代理字符串不可信,是的,您不应该使用浏览器检测来确定在 JS 中运行哪些代码,但是,我希望拥有尽可能准确的统计数据。

我做了以下事情。

我使用 JavaScript 和 PHP 的组合来记录统计数据。 JavaScript 确定浏览器和操作系统(因为这可能是最准确的),PHP 记录它:

JavaScript 来自 Quirksmode,PHP 是不言而喻的。 我使用 MooTools JS 框架。

将以下内容添加到 BrowserDetect 脚本中:

window.addEvent('domready', function() {
    if (BrowserDetect) {
        var q_data = 'ajax=true&browser=' + BrowserDetect.browser + '&version=' + BrowserDetect.version + '&os=' + BrowserDetect.OS;
        var query = 'record_browser.php'
        var req = new Request.JSON({url: query, onComplete: setSelectWithJSON, data: q_data}).post();
    }
});

这将确定用户浏览器的浏览器、浏览器版本和操作系统,并将其发送到 record_browser.php 脚本。 record_browser.php 脚本只是添加信息,以及 PHP session_id 和当前 user_id(如果存在)。

MySQL 表:

CREATE TABLE `browser_detects` (
  `id` int(11) NOT NULL auto_increment,
  `session` varchar(255) NOT NULL default '',
  `user_id` int(11) NOT NULL default '0',
  `browser` varchar(255) NOT NULL default '',
  `version` varchar(255) NOT NULL default '',
  `os` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `sessionUnique` (`session`)
)

PHP 代码:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $session = session_id();
    $user_id = isset($user_id) ? $user_id ? 0;
    $browser = isset($_POST['browser']) ? $_POST['browser'] ? '';
    $version = isset($_POST['version']) ? $_POST['version'] ? '';
    $os = isset($_POST['os']) ? $_POST['os'] ? '';
    $q = $conn->prepare('INSERT INTO browser_detects (`session`, `user`, `browser`, `version`, `os`) VALUES (:session :user, :browser, :version, :os)');
    $q->execute(array(
                    ':session' => $session,
                    ':user' => $user_id,
                    ':browser' => $browser,
                    ':version' => $version,
                    ':os' => $os
                ));
}

I originally asked the question because I want to be able to record the browsers and operations systems people use to access my site. Yes, the user agent string can't be trusted, and yes, you shouldn't use browser detection to determine what code to run in JS, but, I'd like to have as accurate as possible statistics.

I did the following.

I'm using a combination of JavaScript and PHP to record the stats. JavaScript to determine what browser and OS (as this is probably the most accurate), and PHP to record it:

The JavaScript comes from Quirksmode, the PHP is rather self evident. I use the MooTools JS framework.

Add the following to the BrowserDetect script:

window.addEvent('domready', function() {
    if (BrowserDetect) {
        var q_data = 'ajax=true&browser=' + BrowserDetect.browser + '&version=' + BrowserDetect.version + '&os=' + BrowserDetect.OS;
        var query = 'record_browser.php'
        var req = new Request.JSON({url: query, onComplete: setSelectWithJSON, data: q_data}).post();
    }
});

This determines the browser, browser version and OS of the user's browser, and sends it to the record_browser.php script. The record_browser.php script just add's the information, along with the PHP session_id and the current user_id, if present.

MySQL Table:

CREATE TABLE `browser_detects` (
  `id` int(11) NOT NULL auto_increment,
  `session` varchar(255) NOT NULL default '',
  `user_id` int(11) NOT NULL default '0',
  `browser` varchar(255) NOT NULL default '',
  `version` varchar(255) NOT NULL default '',
  `os` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `sessionUnique` (`session`)
)

PHP Code:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $session = session_id();
    $user_id = isset($user_id) ? $user_id ? 0;
    $browser = isset($_POST['browser']) ? $_POST['browser'] ? '';
    $version = isset($_POST['version']) ? $_POST['version'] ? '';
    $os = isset($_POST['os']) ? $_POST['os'] ? '';
    $q = $conn->prepare('INSERT INTO browser_detects (`session`, `user`, `browser`, `version`, `os`) VALUES (:session :user, :browser, :version, :os)');
    $q->execute(array(
                    ':session' => $session,
                    ':user' => $user_id,
                    ':browser' => $browser,
                    ':version' => $version,
                    ':os' => $os
                ));
}
小霸王臭丫头 2024-07-11 17:22:58

编辑:不推荐使用以下解决方案。 试试这个: http://whichbrowser.net/

这曾经对我有用,但现在看看代码,我不知道怎么办。 请使用上面的内容:-/

<script type="text/javascript">
    // <![CDATA[
    var BrowserCheck = Class.create({
        initialize: function () {
            var userAgent = navigator.userAgent.toLowerCase();
            this.version = (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1];
            this.safari = /webkit/.test(userAgent) && !/chrome/.test(userAgent);
            this.opera = /opera/.test(userAgent);
            this.msie = /msie/.test(userAgent) && !/opera/.test(userAgent);
            this.mozilla = /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent);
            this.chrome = /chrome/.test(userAgent);
        }
    });    
    // ]]>
</script>

不要忘记您需要初始化它才能使用它,因此请将其放入您的代码中:

var UserBrowser = new BrowserCheck();

然后检查浏览器类型和版本,如下所示:(例如 Internet Explorer 8)

if ((UserBrowser.msie == true) && (UserBrowser.version == 8))

等。

希望如此这项工作对您来说就像对我们一样,但请记住,没有浏览器检测是万无一失的!

Edit: The solution below isn't recommended. Try this instead: http://whichbrowser.net/

This once worked for me, but looking at the code now, I have no idea how. Use the above instead :-/

<script type="text/javascript">
    // <![CDATA[
    var BrowserCheck = Class.create({
        initialize: function () {
            var userAgent = navigator.userAgent.toLowerCase();
            this.version = (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1];
            this.safari = /webkit/.test(userAgent) && !/chrome/.test(userAgent);
            this.opera = /opera/.test(userAgent);
            this.msie = /msie/.test(userAgent) && !/opera/.test(userAgent);
            this.mozilla = /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent);
            this.chrome = /chrome/.test(userAgent);
        }
    });    
    // ]]>
</script>

Don't forget that you need to initialize it to use it, so put this in your code:

var UserBrowser = new BrowserCheck();

And then check for a browser type and version like so: (e.g. Internet Explorer 8)

if ((UserBrowser.msie == true) && (UserBrowser.version == 8))

etc.

Hope it does the job for you like it has for us, but remember that no browser detection is bullet proof!

智商已欠费 2024-07-11 17:22:58

通常,当浏览器发出请求时,它会向您发送一堆信息(时间、名称日期、用户代理...)。 您应该尝试查看客户端发送的标头,然后转到告诉您他们的浏览器的标头(我认为它是“User-Agent:”。

Generally, when a browser makes a request, it sends a bunch of information to you (time, name date, user-agent...). You should try to look at the headers the client sent and go to the one that tells you their browser (I think it's "User-Agent:".

软糯酥胸 2024-07-11 17:22:58

对于 Internet Explorer 和样式表,您可以使用以下语法:

<!--[if lte IE 6]><link href="/style.css" rel="stylesheet" type="text/css" /><![endif]-->

这适用于 IE 6 或更早版本。
您可以更改 IE 版本,并且还可以:

<!--[if eq IE 7]> = Equal too IE 7
<!--[if gt IE 6]> = Greater than IE 6

我不确定这是否适用于页面的其他部分,但在放置在 标记中时有效。 请参阅此示例了解更多信息

For internet explorer and Style sheets you can use the following syntax:

<!--[if lte IE 6]><link href="/style.css" rel="stylesheet" type="text/css" /><![endif]-->

This applys to IE 6 or earlier.
You can change the IE version and also have:

<!--[if eq IE 7]> = Equal too IE 7
<!--[if gt IE 6]> = Greater than IE 6

Im not sure if this works with other parts of the page but works when placed within the <head> tag. See this example for more information

匿名的好友 2024-07-11 17:22:58

使用 javascript 时:不要使用浏览器检测

编写针对浏览器展示的给定情况进行自我测试的代码,否则您只是为非常小的人群编写代码。 最好在需要时使用 "typeof foo == 'undefined'" 和浏览器特定的技巧。

jQuery 在其代码库中都做到了这一点(如果您查看代码,您会发现它实现了不同浏览器技术的行为)

从长远来看它更好。

When using javascript: Don't use browser detection

Write code that tests itself for given cases exhibited by browsers, otherwise you'll simply be writing code for a very very small population. Its better to use "typeof foo == 'undefined'" and browser specific tricks where you need them.

jQuery does this all over its codebase ( if you look at the code you'll see it implementing behaviours for different browser tecnologies )

Its better in the long run.

各空 2024-07-11 17:22:58

由于我刚刚将其发布在(现已删除的问题)中,并且它仍在我的粘贴缓冲区中,因此我将重新发布。
注意:这是一个服务器端 PHP 解决方案,

我目前使用以下代码。 这并不是一个令人筋疲力尽的解决方案,但实现更多浏览器应该很容易。 我不知道 user-agents.org (感谢 PConroy),“其中一天”我将循环浏览它,看看是否可以更新并添加到我的列表中。

define("BROWSER_OPERA","Opera");
define("BROWSER_IE","IE");
define("BROWSER_OMNIWEB","Omniweb");
define("BROWSER_KONQUEROR","Konqueror");
define("BROWSER_SAFARI","Safari");
define("BROWSER_MOZILLA","Mozilla");
define("BROWSER_OTHER","other");

$aBrowsers = array
(
  array("regexp" => "@Opera(/| )([0-9].[0-9]{1,2})@", "browser" => BROWSER_OPERA, "index" => 2),
  array("regexp" => "@MSIE ([0-9].[0-9]{1,2})@", "browser" => BROWSER_IE, "index" => 1),
  array("regexp" => "@OmniWeb/([0-9].[0-9]{1,2})@", "browser" => BROWSER_OMNIWEB, "index" => 1),
  array("regexp" => "@(Konqueror/)(.*)(;)@", "browser" => BROWSER_KONQUEROR, "index" => 2),
  array("regexp" => "@Safari/([0-9]*)@", "browser" => BROWSER_SAFARI, "index" => 1),
  array("regexp" => "@Mozilla/([0-9].[0-9]{1,2})@", "browser" => BROWSER_MOZILLA, "index" => 1)
);

foreach($aBrowsers as $aBrowser)
{
  if (preg_match($aBrowser["regexp"], $_SERVER["HTTP_USER_AGENT"], $aBrowserVersion))
  {
    define("BROWSER_AGENT",$aBrowser["browser"]);
    define("BROWSER_VERSION",$aBrowserVersion[$aBrowser["index"]]);
    break;
  }
}

Since I just posted this in a (now-deleted question) and it's still in my paste buffer, I'll just repost.
Note: this is a server-side PHP solution

I currently use the following code for this. It is not nearly an exhausting solution, but it should be easy to implement more browsers. I didn't know about user-agents.org (thanks PConroy), "one of these days" I'll loop through it and see if I can update and add to my list.

define("BROWSER_OPERA","Opera");
define("BROWSER_IE","IE");
define("BROWSER_OMNIWEB","Omniweb");
define("BROWSER_KONQUEROR","Konqueror");
define("BROWSER_SAFARI","Safari");
define("BROWSER_MOZILLA","Mozilla");
define("BROWSER_OTHER","other");

$aBrowsers = array
(
  array("regexp" => "@Opera(/| )([0-9].[0-9]{1,2})@", "browser" => BROWSER_OPERA, "index" => 2),
  array("regexp" => "@MSIE ([0-9].[0-9]{1,2})@", "browser" => BROWSER_IE, "index" => 1),
  array("regexp" => "@OmniWeb/([0-9].[0-9]{1,2})@", "browser" => BROWSER_OMNIWEB, "index" => 1),
  array("regexp" => "@(Konqueror/)(.*)(;)@", "browser" => BROWSER_KONQUEROR, "index" => 2),
  array("regexp" => "@Safari/([0-9]*)@", "browser" => BROWSER_SAFARI, "index" => 1),
  array("regexp" => "@Mozilla/([0-9].[0-9]{1,2})@", "browser" => BROWSER_MOZILLA, "index" => 1)
);

foreach($aBrowsers as $aBrowser)
{
  if (preg_match($aBrowser["regexp"], $_SERVER["HTTP_USER_AGENT"], $aBrowserVersion))
  {
    define("BROWSER_AGENT",$aBrowser["browser"]);
    define("BROWSER_VERSION",$aBrowserVersion[$aBrowser["index"]]);
    break;
  }
}
桃酥萝莉 2024-07-11 17:22:58

在服务器上,您几乎只能使用浏览器提供的 UserAgent 字符串(这充满了问题,请阅读 UserAgent 字符串的历史记录)。

在客户端(即在 Javascript 中),您有更多选择。 但最好的选择是实际上不必担心确定它是什么浏览器。 只需检查以确保您想要使用的任何功能确实存在。

例如,您可能想要使用 setCapture,只有 MSIE 提供:

if (element.setCapture) element.setCapture()

我们只是在使用之前查看它是否支持某些内容,而不是弄清楚浏览器是什么,然后推断其功能 - 谁知道哪些浏览器将支持哪些内容将来,如果 Safari 决定支持 setCapture,您是否真的希望必须返回并更新脚本?

On the server you're pretty much limited to the UserAgent string the browser provides (which is fraught with problems, have a read about the UserAgent string's history).

On the client (ie in Javascript), you have more options. But the best option is to not actually worry about working out which browser it is. Simply check to make sure whatever feature you want to use actually exists.

For example, you might want to use setCapture, which only MSIE provides:

if (element.setCapture) element.setCapture()

Rather than working out what the browser is, and then inferring its capabilities, we're simply seeing if it supports something before using it - who knows what browsers will support what in the future, do you really want to have to go back and update your scripts if Safari decides to support setCapture?

用心笑 2024-07-11 17:22:58

JQuery 浏览器插件 将为您在客户端完成此操作。

什么是 jQuery 浏览器插件?

jQuery 浏览器插件是 jQuery 的插件,可以轻松唯一地识别访问者的浏览器。

它有什么作用?

它为您提供了一个 JavaScript 对象,其中包含有关所使用的浏览器的所有信息。 它还添加了 CSS 浏览器选择器,这意味着您可以为特定浏览器、浏览器版本、布局、布局版本甚至操作系统设置元素样式或编写函数。 正在运行的 jQuery 浏览器插件的图像。

该插件使 $.browser 可用,如果服务器端确实需要它,您可以通过 AJAX 调用将其重新提交到服务器。

alert($.browser.name);  // Alerts Firefox for me

然而,该插件的有效性取决于它所测试的浏览器。 上面列出的插件有一个在其测试中识别的浏览器列表,但总是存在风险一个新的浏览器将会悄悄出现(Google Chrome..),这将需要重写识别规则。 也就是说,这个插件似乎会定期更新。

The JQuery Browser Plugin will do it client-side for you.

What is the jQuery Browser Plugin?

The jQuery Browser Plugin is an addon for jQuery that makes it easy to uniquely identify your visitors' browsers.

What does it do?

It gives you an object in javascript that contains all of the information about the browser being used. It also adds CSS browser selectors, which means you can style elements or write functions for specific browsers, browser versions, layouts, layout versions, and even operating systems. Image of the jQuery Browser Plugin in action.

The plug-in makes $.browser available, which you can re-submit to your server via an AJAX call, if you really need it server-side.

alert($.browser.name);  // Alerts Firefox for me

The plug-in will only be as effective as the browsers it's been tested against, however. The plugin listed above has a list of browsers recognised in it's tests, but there's always the risk that a new browser will come sneaking out (Google Chrome..) that will require a re-write of the recognition rules. That said, this plug-in seems to be regularly updated.

戒ㄋ 2024-07-11 17:22:58

这是我使用的C#代码,希望对您有所帮助。

StringBuilder strb = new StringBuilder();
strb.AppendFormat ( "User Agent: {0}{1}", Request.ServerVariables["http_user_agent"].ToString(), Environment.NewLine );
strb.AppendFormat ( "Browser: {0}{1}", Request.Browser.Browser.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Version: {0}{1}", Request.Browser.Version.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Major Version: {0}{1}", Request.Browser.MajorVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Minor Version: {0}{1}", Request.Browser.MinorVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Platform: {0}{1}", Request.Browser.Platform.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "ECMA Script version: {0}{1}", Request.Browser.EcmaScriptVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Type: {0}{1}", Request.Browser.Type.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "-------------------------------------------------------------------------------{0}",  Environment.NewLine );
strb.AppendFormat ( "ActiveX Controls: {0}{1}", Request.Browser.ActiveXControls.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Background Sounds: {0}{1}", Request.Browser.BackgroundSounds.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "AOL: {0}{1}", Request.Browser.AOL.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Beta: {0}{1}", Request.Browser.Beta.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "CDF: {0}{1}", Request.Browser.CDF.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "ClrVersion: {0}{1}", Request.Browser.ClrVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Cookies: {0}{1}", Request.Browser.Cookies.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Crawler: {0}{1}", Request.Browser.Crawler.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Frames: {0}{1}", Request.Browser.Frames.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Tables: {0}{1}", Request.Browser.Tables.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "JavaApplets: {0}{1}", Request.Browser.JavaApplets.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "JavaScript: {0}{1}", Request.Browser.JavaScript.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "MSDomVersion: {0}{1}", Request.Browser.MSDomVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "TagWriter: {0}{1}", Request.Browser.TagWriter.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "VBScript: {0}{1}", Request.Browser.VBScript.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "W3CDomVersion: {0}{1}", Request.Browser.W3CDomVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Win16: {0}{1}", Request.Browser.Win16.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Win32: {0}{1}", Request.Browser.Win32.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "-------------------------------------------------------------------------------{0}", Environment.NewLine );
strb.AppendFormat ( "MachineName: {0}{1}", Environment.MachineName, Environment.NewLine );
strb.AppendFormat ( "OSVersion: {0}{1}", Environment.OSVersion, Environment.NewLine );
strb.AppendFormat ( "ProcessorCount: {0}{1}", Environment.ProcessorCount, Environment.NewLine );
strb.AppendFormat ( "UserName: {0}{1}", Environment.UserName, Environment.NewLine );
strb.AppendFormat ( "Version: {0}{1}", Environment.Version, Environment.NewLine );
strb.AppendFormat ( "UserInteractive: {0}{1}", Environment.UserInteractive, Environment.NewLine );
strb.AppendFormat ( "UserDomainName: {0}{1}", Environment.UserDomainName, Environment.NewLine );

This is the C # code I use, I hope will be helpful.

StringBuilder strb = new StringBuilder();
strb.AppendFormat ( "User Agent: {0}{1}", Request.ServerVariables["http_user_agent"].ToString(), Environment.NewLine );
strb.AppendFormat ( "Browser: {0}{1}", Request.Browser.Browser.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Version: {0}{1}", Request.Browser.Version.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Major Version: {0}{1}", Request.Browser.MajorVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Minor Version: {0}{1}", Request.Browser.MinorVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Platform: {0}{1}", Request.Browser.Platform.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "ECMA Script version: {0}{1}", Request.Browser.EcmaScriptVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Type: {0}{1}", Request.Browser.Type.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "-------------------------------------------------------------------------------{0}",  Environment.NewLine );
strb.AppendFormat ( "ActiveX Controls: {0}{1}", Request.Browser.ActiveXControls.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Background Sounds: {0}{1}", Request.Browser.BackgroundSounds.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "AOL: {0}{1}", Request.Browser.AOL.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Beta: {0}{1}", Request.Browser.Beta.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "CDF: {0}{1}", Request.Browser.CDF.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "ClrVersion: {0}{1}", Request.Browser.ClrVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Cookies: {0}{1}", Request.Browser.Cookies.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Crawler: {0}{1}", Request.Browser.Crawler.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Frames: {0}{1}", Request.Browser.Frames.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Tables: {0}{1}", Request.Browser.Tables.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "JavaApplets: {0}{1}", Request.Browser.JavaApplets.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "JavaScript: {0}{1}", Request.Browser.JavaScript.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "MSDomVersion: {0}{1}", Request.Browser.MSDomVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "TagWriter: {0}{1}", Request.Browser.TagWriter.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "VBScript: {0}{1}", Request.Browser.VBScript.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "W3CDomVersion: {0}{1}", Request.Browser.W3CDomVersion.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Win16: {0}{1}", Request.Browser.Win16.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "Win32: {0}{1}", Request.Browser.Win32.ToString ( ), Environment.NewLine );
strb.AppendFormat ( "-------------------------------------------------------------------------------{0}", Environment.NewLine );
strb.AppendFormat ( "MachineName: {0}{1}", Environment.MachineName, Environment.NewLine );
strb.AppendFormat ( "OSVersion: {0}{1}", Environment.OSVersion, Environment.NewLine );
strb.AppendFormat ( "ProcessorCount: {0}{1}", Environment.ProcessorCount, Environment.NewLine );
strb.AppendFormat ( "UserName: {0}{1}", Environment.UserName, Environment.NewLine );
strb.AppendFormat ( "Version: {0}{1}", Environment.Version, Environment.NewLine );
strb.AppendFormat ( "UserInteractive: {0}{1}", Environment.UserInteractive, Environment.NewLine );
strb.AppendFormat ( "UserDomainName: {0}{1}", Environment.UserDomainName, Environment.NewLine );
家住魔仙堡 2024-07-11 17:22:58

正如许多人所说,浏览器检测可能会出错......但是为了 Code Golf 的利益。

这是检测 IE 的一种非常快速的方法。

<script>
  if('\v'=='v'){
    alert('I am IE');
  } else {
    alert('NOT IE');
  }
</script>

实际上它非常简洁,因为它可以识别 IE,而不会在 Opera 上出错。

如果您知道为什么这在 IE 中有效,那就加分了。 ;-)

As stated by many, browser detection can go very wrong... however in the interests of Code Golf.

This is a very fast way to detect IE.

<script>
  if('\v'=='v'){
    alert('I am IE');
  } else {
    alert('NOT IE');
  }
</script>

Its pretty neat actually because it picks out IE without tripping up on Opera.

Bonus points if you know why this works in IE. ;-)

染年凉城似染瑾 2024-07-11 17:22:58

不要使用浏览器检测:

  • 浏览器检测在最好的情况下也不是 100% 可靠,但情况会变得更糟:
  • 浏览器有很多变体(MSIE 自定义等)
  • 浏览器可以谎报自己的身份(Opera 实际上有这个内置功能)
  • 网关隐藏或混淆浏览器的身份
  • 自定义和网关供应商在 USER_AGENT 中编写自己的垃圾

最好在客户端脚本中进行功能检测。 您希望只需要浏览器检测来解决特定浏览器和版本中的错误。

Don't use browser detection:

  • Browser detection is not 100% reliable at the best of times, but things get worse than this:
  • There are lots of variants of browsers (MSIE customisations etc)
  • Browsers can lie about their identity (Opera actually has this feature built-in)
  • Gateways hide or obfuscate the browser's identity
  • Customisation and gateway vendors write their own rubbish in the USER_AGENT

It's better to do feature detection in client-script. You hopefully only need browser-detection to work around a bug in a specific browser and version.

把人绕傻吧 2024-07-11 17:22:58

正如 Dan 所说:这取决于所使用的技术。

对于 PHP 服务器端浏览器检测,我推荐 Harald Hope 的浏览器检测:

http://techpatterns.com/downloads/php_browser_detection .php

根据 GPL 发布。

as Dan said: it depends on the used technology.

For PHP server side browser detection i recommend Harald Hope's Browser detection:

http://techpatterns.com/downloads/php_browser_detection.php

Published under GPL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文