Extract browser, OS, and device information from a user agent string
Code
Utilitiesfunction parseUserAgent(userAgent) {
const ua = userAgent || navigator.userAgent;
// Detect browser
let browser = 'Unknown';
if (ua.includes('Firefox/')) browser = 'Firefox';
else if (ua.includes('Edg/')) browser = 'Edge';
else if (ua.includes('Chrome/')) browser = 'Chrome';
else if (ua.includes('Safari/') && !ua.includes('Chrome')) browser = 'Safari';
else if (ua.includes('Opera') || ua.includes('OPR/')) browser = 'Opera';
// Detect OS
let os = 'Unknown';
if (ua.includes('Windows')) os = 'Windows';
else if (ua.includes('Mac OS')) os = 'macOS';
else if (ua.includes('Linux')) os = 'Linux';
else if (ua.includes('Android')) os = 'Android';
else if (ua.includes('iPhone') || ua.includes('iPad')) os = 'iOS';
// Detect device type
let device = 'Desktop';
if (/Mobile|Android|iPhone|iPad|iPod/i.test(ua)) {
device = /iPad|Tablet/i.test(ua) ? 'Tablet' : 'Mobile';
}
// Extract version
const versionMatch = ua.match(/(Chrome|Firefox|Safari|Edge|OPR)\/(\d+)/);
const version = versionMatch ? versionMatch[2] : null;
return { browser, os, device, version, raw: ua };
}
return parseUserAgent(ua);Parameters
User agent string (leave empty to use current browser)
Browser·fetch() may be limited by CORS
Example Output
For a Chrome browser on macOS:
{
"browser": "Chrome",
"os": "macOS",
"device": "Desktop",
"version": "120",
"raw": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)..."
}
Common User Agents
| Browser | Identifier |
|---|---|
| Chrome | Chrome/ |
| Firefox | Firefox/ |
| Safari | Safari/ (without Chrome) |
| Edge | Edg/ |
| Opera | OPR/ or Opera |
Bot Detection
function isBot(userAgent) {
const botPatterns = [
/bot/i, /crawl/i, /spider/i, /slurp/i,
/googlebot/i, /bingbot/i, /yandex/i,
/facebookexternalhit/i, /twitterbot/i
];
return botPatterns.some(p => p.test(userAgent));
}
More JavaScript Snippets
Add Query Parameter
Add or update a query parameter in a URL string.
Bearer Token Authentication
Make an authenticated HTTP request using Bearer token for JWT or OAuth.
Check Cloudflare Cache Status
Check if a resource is served from Cloudflare's cache by inspecting the CF-Cache-Status header.
CORS Cross-Origin Request
Make a cross-origin HTTP request with CORS headers inspection.
Delayed Response (Test Timeouts)
Request a delayed response to test timeout handling with AbortController.
Download Binary File
Download an image or binary file and convert it to a blob or base64.