--- title: 사용자 에이전트를 이용한 브라우저 감지 slug: Web/HTTP/Browser_detection_using_the_user_agent tags: - Compatibility - HTTP - Web Development translation_of: Web/HTTP/Browser_detection_using_the_user_agent original_slug: Web/HTTP/User_agent를_이용한_브라우저_감지 ---
보통 브라우저마다 다른 웹 페이지 또는 서비스를 제공하는 것은 나쁜 생각입니다. 웹은 사용자가 어떤 브라우저나 디바이스를 사용하고 있는지 개의치 않고 모두에게 접근성이 용이해야 하기 때문입니다. 따라서 특정 브라우저를 타겟으로 개발하는 것보다 가용적인 기능들 (예를 들어 Web API 등)을 이용하여 당신의 웹 사이트를 개선하는 것을 추천합니다.
하지만 브라우저와 웹 표준은 완벽하지 않고 그 간극은 여전히 브라우저 감지 기능을 필요로 합니다. User-Agent를 사용하여 브라우저를 감지하는 것은 간단해 보이지만, 사실 그것을 잘 이용하는 것은 무척 힘든 일입니다. 이 문서는 사용자 에이전트를 이용하여 브라우저를 바르게 감지하도록 안내합니다.
주의하세요! user agent 정보를 가로채는 것은 좋은 아이디어가 아닙니다. 대부분의 경우 호환성이 뛰어난 좋은 다른 해결방안을 찾을 수 있을 것입니다.
사용자 에이전트 문자열을 이용해 브라우저를 감지하기 전에 가능하다면 이것을 사용하지 않는 것이 첫 번째입니다. 내가 왜 이 기능을 원하는지 다시 한 번 스스로 확인하길 바랍니다.
user agent 감지를 피하는 몇 가지 방법이 있습니다!
// 아래 코드 조각은 한 문자열을 특별한 표기법으로 쪼갭니다. if (navigator.userAgent.indexOf("Chrome") !== -1){ // 네! 이 사용자가 정규표현식의 look-behind 기능을 사용하려는 것 // 같습니다. // /(?<=[A-Z])/를 사용하지 마십시오. 정규표현식의 // look-behind 기능을 지원하지 않는 브라우저에서는 문법오류가 // 발생할 것입니다. 왜냐하면, 모든 브라우저들은 실제로 실행되지 // 않는 부분을 포함한 전체 스크립트를 해석하기 때문입니다. var camelCaseExpression = new RegExp("(?<=[A-Z])"); var splitUpString = function(str) { return (""+str).split(camelCaseExpression); }; } else { /*아래 fallback 코드는 성능이 떨어지지만 작동하긴 합니다.*/ var splitUpString = function(str){ return str.replace(/[A-Z]/g,"z$1").split(/z(?=[A-Z])/g); }; } console.log(splitUpString("fooBare")); // ["fooB", "are"] console.log(splitUpString("jQWhy")); // ["jQ", "W", "hy"]
위의 코드는 다음과 같은 몇 가지 잘못된 가정을 했습니다.
look-behind 지원여부 자체를 테스트함으로써 이 문제들을 회피할 수 있습니다.
var isLookBehindSupported = false; try { new RegExp("(?<=)"); isLookBehindSupported = true; } catch (err) { // agent가 look-behind 기능을 지원하지 않는다면, 위 문법을 사용한 // RegExp 객체의 생성이 에러를 던질 것이고, isLookBehindSupported는 // 여전히 false일 것입니다. } var splitUpString = isLookBehindSupported ? function(str) { return (""+str).split(new RegExp("(?<=[A-Z])")); } : function(str) { return str.replace(/[A-Z]/g,"z$1").split(/z(?=[A-Z])/g); };
위의 코드가 시범을 보였듯이, user agent를 살펴보지 않고도 어떤 기능에 대한 브라우저 지원 여부를 시험할 수 있는 방법이 항상 존재합니다. 기능 지원 여부를 확인하기 위해 user agent 문자열을 확인할 필요가 전혀 없습니다.
마지막으로, 위의 코드 조각은 크로스 브라우저 코딩과 관련하여 항상 염두에 두어야만 하는 중요한 이슈를 시사합니다.테스트 중인 API를 해당 기능이 지원되지 않는 브라우저에서 실수로 사용하지 마십시오. 확실하고 단순한 이야기 같지만, 때때로 그렇지 않습니다. 예를 들어, 위의 코드 조각에서, lookbehind 기능을 short-regexp 표기법(예를 들어, /reg/igm)으로 사용한다면, 지원되지 않는 브라우저에서는 파싱 에러가 발생할 것입니다. 따라서, 위의 예제에서, /(?<=look_behind_stuff)/ 대신에 new RegExp("(?<=look_behind_stuff)")를 사용하는 것이 좋습니다. 심지어 lookbehind가 지원되는 조건분기의 코드에서도 말입니다.
As there is no uniformity of the different part of the user agent string, this is the tricky part.
사람들이 "브라우저 감시(browser detection)"을 원한다고 말할 때, 실제로 대부분 그들은 "렌더링 엔진 탐지(rendering engine detection)"를 원합니다. Do you actually want to detect Firefox, as opposed to SeaMonkey, or Chrome as opposed to Chromium? Or do you actually simply want to see if the browser is using the Gecko or the WebKit rendering engine? If this is what you need, see further down the page.
Most browser set the name and version in the format BrowserName/VersionNumber, with the notable exception of Internet Explorer. But as the name is not the only information in a user agent string that is in that format, you can not discover the name of the browser, you can only check if the name you are looking for. But note that some browsers are lying: Chrome for example reports both as Chrome and Safari. So to detect Safari you have to check for the Safari string and the absence of the Chrome string, Chromium often reports itself as Chrome too or Seamonkey sometimes reports itself as Firefox.
Also pay attention not to use a simple regular expression on the BrowserName, user agents also contain strings outside the Keyword/Value syntax. Safari & Chrome contain the string 'like Gecko', for instance.
반드시 포함 | 반드시 포함하지 않음 | ||
---|---|---|---|
Firefox | Firefox/xyz | Seamonkey/xyz | |
Seamonkey | Seamonkey/xyz | ||
Chrome | Chrome/xyz | Chromium/xyz | |
Chromium | Chromium/xyz | ||
Safari | Safari/xyz | Chrome/xyz or Chromium/xyz | Safari gives two version number, one technical in the Safari/xyz token, one user-friendly in a Version/xyz token |
Opera |
OPR/xyz [1] Opera/xyz [2] |
[1] Opera 15+ (Blink-based engine) [2] Opera 12- (Presto-based engine) |
|
Internet Explorer | ; MSIE xyz; | Internet Explorer doesn't put its name in the BrowserName/VersionNumber format |
Of course, there is absolutely no guarantee that another browser will not hijack some of these things (like Chrome hijacked the Safari string in the past). That's why browser detection using the user agent string is unreliable and should be done only with the check of version number (hijacking of past versions is less likely).
The browser version is often, but not always, put in the value part of the BrowserName/VersionNumber token in the User Agent String. This is of course not the case for Internet Explorer (which puts the version number right after the MSIE token), and for Opera after version 10, which has added a Version/VersionNumber token.
Here again, be sure to take the right token for the browser you are looking for, as there is no guarantee that others will contain a valid number.
As seen earlier, in most cases, looking for the rendering engine is a better way to go. This will help to not exclude lesser known browsers. Browsers sharing a common rendering engine will display a page in the same way: it is often a fair assumption that what will work in one will work in the other.
There are five major rendering engines: Trident, Gecko, Presto, Blink and WebKit. As sniffing the rendering engines names is common, a lot of user agents added other rendering names to trigger detection. It is therefore important to pay attention not to trigger false-positives when detecting the rendering engine.
반드시 포함 | ||
---|---|---|
Gecko | Gecko/xyz | |
WebKit | AppleWebKit/xyz | Pay attention, WebKit browsers add a 'like Gecko' string that may trigger false positive for Gecko if the detection is not careful. |
Presto | Opera/xyz | Note: Presto is no longer used in Opera browser builds >= version 15 (see 'Blink') |
Trident | Trident/xyz | Internet Explorer put this token in the comment part of the User Agent String |
Blink | Chrome/xyz |
Most rendering engine put the version number in the RenderingEngine/VersionNumber token, with the notable exception of Gecko. Gecko puts the Gecko version number in the comment part of the User Agent after the rv:
string. From Gecko 14 for the mobile version and Gecko 17 for the desktop version, it also puts this value in the Gecko/version
token (previous version put there the build date, then a fixed date called the GeckoTrail).
The Operating System is given in most User Agent strings (although not web-focussed platforms like Firefox OS), but the format varies a lot. It is a fixed string between two semi-colons, in the comment part of the User Agent. These strings are specific for each browsers. They indicates the OS, but also often its version and information on the relying hardware (32 or 64 bits, or Intel/PPC for Mac).
Like in all cases, these strings may change in the future, one should use them only in conjunction for the detection of already released browsers. A technological survey must be in place to adapt the script when new browser versions are coming out.
The most common reason to perform user agent sniffing is to determine which type of device the browser runs on. The goal is to serve different HTML to different device types.
The following table summarizes the way major browser vendors indicate that their browsers are running on a mobile device:
브라우저 | 규칙 | 예제 |
---|---|---|
Mozilla (Gecko, Firefox) | Mobile or Tablet token in the comment. | Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0 |
WebKit-based (Android, Safari) | Mobile Safari token outside the comment. | Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 |
Blink-based (Chromium, Google Chrome, Opera 15+) | Mobile Safari token outside the comment | Mozilla/5.0 (Linux; Android 4.4.2); Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36 OPR/20.0.1396.72047 |
Presto-based (Opera 12-) |
Opera Mobi/xyz token in the comment (Opera 12-) |
Opera/9.80 (Android 2.3.3; Linux; Opera Mobi/ADR-1111101157; U; es-ES) Presto/2.9.201 Version/11.50 |
Internet Explorer | IEMobile/xyz token in the comment. | Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0) |
In summary, we recommend looking for the string “Mobi” anywhere in the User Agent to detect a mobile device.
If the device is large enough that it's not marked with “Mobi”, you should serve your desktop site (which, as a best practice, should support touch input anyway, as more desktop machines are appearing with touchscreens).