Determining the Operating System Details Using JavaScript
JavaScript provides various mechanisms to determine the user's operating system (OS) name and version:
Navigator Object:
Platform Object:
To obtain both the OS name and version using JavaScript, you can employ the following code snippet:
const osName = navigator.userAgent.match(/Mac|Win|Linux/g).join(''); const osVersion = navigator.userAgent.match(/\d+(\.\d+)+/g).join('.');
This code identifies the OS name (e.g., "Mac", "Win", or "Linux") and its version (e.g., "10.15.1") by extracting and combining the relevant data from the user agent string.
Here's an example of using this code to detect and display the OS details:
const osInfo = getOSInfo(); console.log(`OS Name: ${osInfo.name}`); console.log(`OS Version: ${osInfo.version}`); function getOSInfo() { const osName = navigator.userAgent.match(/Mac|Win|Linux/g).join(''); const osVersion = navigator.userAgent.match(/\d+(\.\d+)+/g).join('.'); return { name: osName, version: osVersion }; }
This approach provides a simple and reliable way to access the OS details in JavaScript.
The above is the detailed content of How Can I Detect the User's Operating System Name and Version Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!