HTML Navigator userAgent Property

The navigator.userAgent property in HTML returns a string containing the complete user-agent header that the browser sends to web servers. This string contains information about the browser name, version, operating system, and rendering engine, making it useful for browser detection and compatibility checks.

Syntax

Following is the syntax for the navigator userAgent property −

navigator.userAgent

Return Value

The property returns a string representing the user-agent header. This string typically includes the browser name, version, operating system, and other identifying information that the browser reports to web servers.

Example − Basic userAgent Display

Following example demonstrates how to retrieve and display the browser's user-agent string −

<!DOCTYPE html>
<html>
<head>
   <title>Navigator userAgent Property</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Browser User-Agent Information</h2>
   <button onclick="showUserAgent()" style="padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;">Show User-Agent</button>
   <div id="result" style="margin-top: 20px; padding: 15px; background-color: #f8f9fa; border-radius: 5px;"></div>

   <script>
      function showUserAgent() {
         const userAgent = navigator.userAgent;
         document.getElementById("result").innerHTML = 
            "<h3>User-Agent String:</h3>" +
            "<p style='word-break: break-all; font-family: monospace; background: white; padding: 10px; border: 1px solid #ddd;'>" + 
            userAgent + "</p>";
      }
   </script>
</body>
</html>

The output displays a button that, when clicked, shows the complete user-agent string −

Browser User-Agent Information
[Show User-Agent]

(After clicking, displays the user-agent string like:)
User-Agent String:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

Example − Browser Detection

Following example shows how to use the userAgent property to detect different browsers −

<!DOCTYPE html>
<html>
<head>
   <title>Browser Detection with userAgent</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Browser Detection Demo</h2>
   <button onclick="detectBrowser()" style="padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">Detect Browser</button>
   <div id="browserInfo" style="margin-top: 20px; padding: 15px; background-color: #e9ecef; border-radius: 5px;"></div>

   <script>
      function detectBrowser() {
         const userAgent = navigator.userAgent;
         let browserName = "Unknown Browser";
         let browserIcon = "?";

         if (userAgent.includes("Chrome") && !userAgent.includes("Edg")) {
            browserName = "Google Chrome";
            browserIcon = "?";
         } else if (userAgent.includes("Firefox")) {
            browserName = "Mozilla Firefox";
            browserIcon = "?";
         } else if (userAgent.includes("Safari") && !userAgent.includes("Chrome")) {
            browserName = "Apple Safari";
            browserIcon = "?";
         } else if (userAgent.includes("Edg")) {
            browserName = "Microsoft Edge";
            browserIcon = "?";
         } else if (userAgent.includes("Opera") || userAgent.includes("OPR")) {
            browserName = "Opera";
            browserIcon = "?";
         }

         document.getElementById("browserInfo").innerHTML = 
            "<h3>" + browserIcon + " Detected Browser: " + browserName + "</h3>" +
            "<p><strong>Full User-Agent:</strong></p>" +
            "<p style='font-family: monospace; font-size: 12px; background: white; padding: 10px; border: 1px solid #ccc; word-break: break-all;'>" + 
            userAgent + "</p>";
      }
   </script>
</body>
</html>

This example analyzes the user-agent string to identify the browser and displays both the detected browser name and the complete user-agent string.

Example − Operating System Detection

Following example demonstrates how to extract operating system information from the userAgent string −

<!DOCTYPE html>
<html>
<head>
   <title>OS Detection with userAgent</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Operating System Detection</h2>
   <button onclick="detectOS()" style="padding: 10px 20px; background-color: #6f42c1; color: white; border: none; border-radius: 5px; cursor: pointer;">Detect Operating System</button>
   <div id="osInfo" style="margin-top: 20px; padding: 15px; background-color: #f3e5f5; border-radius: 5px;"></div>

   <script>
      function detectOS() {
         const userAgent = navigator.userAgent;
         let osName = "Unknown OS";
         let osIcon = "?";

         if (userAgent.includes("Windows NT 10.0")) {
            osName = "Windows 10/11";
            osIcon = "?";
         } else if (userAgent.includes("Windows NT 6.3")) {
            osName = "Windows 8.1";
            osIcon = "?";
         } else if (userAgent.includes("Windows NT 6.2")) {
            osName = "Windows 8";
            osIcon = "?";
         } else if (userAgent.includes("Windows NT 6.1")) {
            osName = "Windows 7";
            osIcon = "?";
         } else if (userAgent.includes("Mac OS X")) {
            osName = "macOS";
            osIcon = "?";
         } else if (userAgent.includes("Linux")) {
            osName = "Linux";
            osIcon = "?";
         } else if (userAgent.includes("Android")) {
            osName = "Android";
            osIcon = "?";
         } else if (userAgent.includes("iPhone") || userAgent.includes("iPad")) {
            osName = "iOS";
            osIcon = "?";
         }

         document.getElementById("osInfo").innerHTML = 
            "<h3>" + osIcon + " Detected OS: " + osName + "</h3>" +
            "<p><strong>User-Agent Analysis:</strong></p>" +
            "<p style='font-family: monospace; font-size: 12px; background: white; padding: 10px; border: 1px solid #ccc; word-break: break-all;'>" + 
            userAgent + "</p>";
      }
   </script>
</body>
</html>

This example parses the user-agent string to identify the operating system and displays the result along with the complete user-agent information.

User-Agent String Components Mozilla/5.0 (Application name) (Windows NT 10.0; Win64; x64) (Platform details) AppleWebKit/537.36 (KHTML, like Gecko) (Rendering engine) Chrome/91.0.4472.124 (Browser version)

Common Use Cases

The navigator.userAgent property is commonly used for −

  • Browser Detection − Identifying specific browsers for compatibility or feature support.

  • Operating System Detection − Determining the user's OS for platform-specific functionality.

  • Mobile Device Detection − Detecting mobile browsers for responsive design adaptations.

  • Analytics and Logging − Recording browser and system information for website analytics.

  • Feature Detection Alternative − Though feature detection is preferred, user-agent can be used as a fallback.

Important Considerations

When using the userAgent property, keep in mind −

  • User-agent spoofing − Users or browsers can modify the user-agent string, making it unreliable for security purposes.

  • Frequent changes − Browser vendors regularly update user-agent strings, which may break detection logic.

  • Feature detection preferred − Instead of browser detection, it's often better to use feature detection with methods like typeof or in operator.

  • Privacy concerns − Some browsers are moving toward reducing user-agent information to protect user privacy.

Conclusion

The navigator.userAgent property provides valuable information about the user's browser and system environment. While useful for browser detection and analytics, it should be used carefully due to reliability concerns and the preference for feature detection in modern web development. Always consider the limitations and potential privacy implications when implementing user-agent based logic.

Updated on: 2026-03-16T21:38:54+05:30

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements