如何通过网页获取Windows系统版本,包括19043.1110这类具体版本号?
How to Get Detailed Windows System Version (e.g., 19043.1110) via a Web Page
Hey there! If you need to pull that specific, granular Windows build number (like 19043.1110) directly from a web page, here are two solid JavaScript-based methods—no need to send users to their system settings or external tools.
1. Parse the User Agent String (Works Everywhere)
The tried-and-true approach is to extract info from the navigator.userAgent string, which all browsers include in their requests. Here's a script tailored to grab the exact build number:
function getWindowsBuildInfo() { const userAgent = navigator.userAgent; // Match patterns like "Windows NT 10.0; Win64; x64; ... Build 19043.1110" const match = userAgent.match(/Windows NT (\d+\.\d+);.*Build (\d+\.\d+)/); if (match) { return { ntVersion: match[1], // e.g., "10.0" for Windows 10/11 fullBuildNumber: match[2] // e.g., "19043.1110" }; } return null; // Not running on Windows } // Example usage const windowsDetails = getWindowsBuildInfo(); if (windowsDetails) { console.log(`Windows NT Version: ${windowsDetails.ntVersion}`); console.log(`Exact Build: ${windowsDetails.fullBuildNumber}`); } else { console.log("This isn't a Windows system, or the user agent is modified."); }
Pros & Cons:
- Pros: Compatible with all browsers, no modern API requirements.
- Cons: User agent strings can be modified by users or browsers, so results aren't 100% guaranteed. You might need to tweak the regex slightly for edge cases (like older Windows versions).
2. Use User-Agent Client Hints (Modern, Reliable)
For newer browsers (Chrome 89+, Edge 89+, etc.), the navigator.userAgentData API provides structured system information, so you don't have to mess with regex. Here's how to get the full build details:
async function getWindowsBuildWithClientHints() { if (!navigator.userAgentData) { return null; // Browser doesn't support Client Hints } // Request the detailed platform version const highEntropyValues = await navigator.userAgentData.getHighEntropyValues([ "platformVersion" ]); if (navigator.userAgentData.platform === "Windows") { // platformVersion returns something like "10.0.19043.1110" const [major, minor, build, revision] = highEntropyValues.platformVersion.split("."); return { ntVersion: `${major}.${minor}`, fullBuild: `${build}.${revision}`, completeVersion: highEntropyValues.platformVersion }; } return null; } // Example usage getWindowsBuildWithClientHints().then(details => { if (details) { console.log(`Windows NT Version: ${details.ntVersion}`); console.log(`Full Detailed Version: ${details.completeVersion}`); console.log(`Specific Build: ${details.fullBuild}`); } else { console.log("Either not on Windows, or your browser doesn't support Client Hints."); } });
Pros & Cons:
- Pros: Structured data, no regex needed, more reliable than parsing user agents.
- Cons: Only works on modern browsers—you'll need to fall back to the first method for older ones.
Quick Notes
- Privacy Heads-Up: Some users disable client hints or modify their user agent for privacy. Don't use this info for critical site functionality that's required for users to access your content.
- Windows 11 Distinction: Both methods will report Windows 11 as "Windows NT 10.0". To tell it apart, check if the build number is 22000 or higher (Windows 11's first build is 22000).
内容的提问来源于stack exchange,提问作者life what
相关产品推荐
相关产品推荐

