Below is a complete, self-contained example of a simple UI built with vanilla JavaScript. This page will display various pieces of information from the navigator object, including the user agent string. I'll focus on key properties related to user info (like browser, OS, and device details) for a clean and informative display.
Explanation:
- HTML Structure: A basic page with a heading and a container div where the information will be dynamically inserted.
- JavaScript: On page load, it fetches properties from the
navigator object and populates the container with a formatted list.
- Properties Displayed: I'll include:
navigator.userAgent (the main user agent string).
- Other relevant properties like
appName, appVersion, platform, vendor, language, languages, onLine, and hardwareConcurrency.
- Error Handling: For properties that might not be available (e.g., in some browsers), I'll use fallbacks like "N/A" or "Unknown".
- Styling: I'll add basic CSS for readability.
Save the following code in two files: index.html and script.js. Open index.html in a browser to see it in action.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Information</title>
<style>
/* Basic styling for better readability */
body {
font-family: Arial, sans-serif;
margin: 20px;
max-width: 800px;
}
h1 {
color: #333;
}
dl {
border: 1px solid #ddd;
padding: 10px;
background-color: #f9f9f9;
}
dt {
font-weight: bold;
color: #555;
margin-top: 10px;
}
dd {
margin: 5px 0 0 20px;
color: #000;
word-break: break-word; /* Helps with long strings like userAgent */
}
</style>
</head>
<body>
<h1>Your Browser and Device Information</h1>
<div id="info-container"></div>
<script src="script.js"></script>
</body>
</html>
script.js
document.addEventListener('DOMContentLoaded', function() {
const container = document.getElementById('info-container');
const nav = navigator; // Reference to the navigator object
// Function to safely get navigator properties with fallbacks
const getNavProperty = (property) => {
try {
return nav[property] !== undefined ? nav[property] : 'N/A';
} catch (error) {
return 'N/A'; // In case the property is restricted
}
};
// Build the HTML string for the info display
const infoHTML = `
<dl>
<dt>User Agent</dt>
<dd>${getNavProperty('userAgent')}</dd>
<dt>App Name</dt>
<dd>${getNavProperty('appName')}</dd>
<dt>App Version</dt>
<dd>${getNavProperty('appVersion')}</dd>
<dt>Platform</dt>
<dd>${getNavProperty('platform')}</dd>
<dt>Vendor</dt>
<dd>${getNavProperty('vendor') || 'Unknown'}</dd>
<dt>Language</dt>
<dd>${getNavProperty('language')}</dd>
<dt>Languages</dt>
<dd>${Array.isArray(getNavProperty('languages')) ? getNavProperty('languages').join(', ') : 'N/A'}</dd>
<dt>Online Status</dt>
<dd>${getNavProperty('onLine') ? 'Yes' : 'No'}</dd>
<dt>Hardware Concurrency (CPU cores)</dt>
<dd>${getNavProperty('hardwareConcurrency')}</dd>
</dl>
`;
// Insert the HTML into the container
container.innerHTML = infoHTML;
});
How to Use:
- Create the two files (
index.html and script.js) in the same folder.
- Open
index.html in a web browser.
- The page will automatically load and display your browser and device information based on the
navigator object.
Notes:
- Security Considerations: Some
navigator properties (like userAgent) can be spoofed or restricted in certain environments (e.g., privacy-focused browsers). This code simply reads what's available.
- Why These Properties?: You mentioned "useragent and navigator' useragent," so I focused on
userAgent and expanded to other related properties for a comprehensive view. If you want to add or remove any, you can modify the infoHTML string in script.js.
- Vanilla JS Only: This uses only plain JavaScript, with no libraries or frameworks.
- Testing: Try this on different browsers (e.g., Chrome, Firefox, Safari) to see variations in the output.
If you need any modifications, like adding more properties or improving the UI, let me know!