網站可透過 UA-CH) (用戶端提示 User-Agent 區分 Windows 11 與 Windows 10 使用者,並偵測裝置的 CPU 架構。 User-Agent 用戶端提示格式被瀏覽器用來向網站提供使用者代理資訊。
網站也可以利用瀏覽器傳送的用戶代理資訊來偵測以下資訊:
- 瀏覽器品牌。
- 瀏覽器版本號。
- 瀏覽器所運行的裝置平台。
網站有兩種方式可以存取使用者代理資訊:
- User-Agent 弦 (傳承) 。
- User-Agent 客戶提示 (推薦) 。
關於這兩種方法的詳細資訊,請參見「 從您的網站偵測 Microsoft Edge」。
在 Microsoft Edge (以及 Google Chrome) ,網站可以區分 Windows 11 與 Windows 10 的使用者,並透過 UA-CH) (用戶端提示 User-Agent 偵測裝置的 CPU 架構。 此資訊可在以下 UA-CH 請求標頭中找到:
| 標頭欄位 | 表示 Windows 10 的數值 | 表示 Windows 11 的數值 |
|---|---|---|
Sec-CH-UA-Platform |
Windows |
Windows |
Sec-CH-UA-Platform-Version |
介於 1.0.0 與 之間的值 10.0.0 |
13.0.0 以及以上 |
User-Agent 字串不會更新以區分 Windows 11 與 Windows 10,或區分 CPU 架構。 我們不建議使用 User-Agent 字串來擷取使用者代理資料。 不支援 User-Agent 客戶端提示的瀏覽器無法區分Windows 11與Windows 10,或是CPU架構。
支援 User-Agent 客戶端提示的瀏覽器
下表顯示哪些瀏覽器支援區分 Windows 11 與 Windows 10,以及不同 CPU 架構。
| 瀏覽器 | 支援透過 User-Agent 客戶端提示來區分? |
|---|---|
| Microsoft Edge | 是 |
| Chrome | 是 |
| Opera | 是 |
| Firefox | 否 |
| Internet Explorer 11 | 否 |
偵測 Windows 11 的範例程式碼
以下程式碼偵測 Windows 11:
navigator.userAgentData.getHighEntropyValues(["platformVersion"])
.then(ua => {
if (navigator.userAgentData.platform === "Windows") {
const majorPlatformVersion = parseInt(ua.platformVersion.split('.')[0]);
if (majorPlatformVersion >= 13) {
console.log("Windows 11 or later");
}
else if (majorPlatformVersion > 0) {
console.log("Windows 10");
}
else {
console.log("Before Windows 10");
}
}
else {
console.log("Not running on Windows");
}
});
偵測 ARM 或 x86 的範例程式碼
利用 CPU 架構偵測功能,讓你的網站自動下載專為使用者 CPU 設計的應用程式版本。
CPU 偵測對於基於 ARM 的裝置特別有用,因此使用 ARM 裝置的客戶會自動下載應用程式的原生 ARM 版本。 這可以避免使用者不小心安裝了為 x86 設計的應用程式,然後因為模擬而導致效能下降。
以下程式碼可偵測 CPU 架構:
navigator.userAgentData.getHighEntropyValues(["architecture", "bitness"])
.then(ua => {
if (navigator.userAgentData.platform === "Windows") {
if (ua.architecture === 'x86') {
if (ua.bitness === '64') {
console.log("x86_64");
}
else if (ua.bitness === '32') {
console.log("x86");
}
}
else if (ua.architecture === 'arm') {
if (ua.bitness === '64') {
console.log("ARM64");
}
else if (ua.bitness === '32') {
console.log("ARM32");
}
}
}
else {
console.log("Not running on Windows");
}
});
優化偵測效能的方法 Critical-CH
目前,網站伺服器必須將回應標頭傳送 Accept-CH 給瀏覽器客戶端,以請求標頭中預設未傳送 Sec-CH-UA 的高熵欄位。 下圖顯示瀏覽器向伺服器發送包含 user agent: <UA string> 包含 的回應標頭,並接收包含 Accept-CH: sec-ch-ua-platform的回應標頭。
在此初始請求中,客戶端會記錄 Accept-CH 偏好,並在後續請求中預設包含 sec-ch-ua-platform 。
為了進一步優化此流程, Critical-CH 新增標頭可與標頭同時 Accept-CH 使用,立即重新發布請求標頭,無需重新載入頁面。 下圖顯示瀏覽器向伺服器發送包含user agent: <UA string>包含 和 Critical-CH: sec-ch-ua-platform的回應標頭的請求標頭,並接收響應Accept-CH: sec-ch-ua-platform標頭。 瀏覽器隨即將請求標頭傳送給伺服器。
你可以用標 Critical-CH 頭接收想要的高熵標頭,並優化效能。
請記住,Critical-CHAccept-CH偏好設定會持續存在,直到會話 Cookie 清除,或使用者清除特定來源的網站資料或 Cookie。 欲了解更多資訊 Critical-CH,請參閱 客戶端提示可靠性。
偵測特定 Windows 版本
API 回傳navigator.userAgentData.getHighEntropyValues的"platformVersion"平台版本定義,透過 User-Agent Client Hints 社群群組報告草稿, (以及標Sec-CH-UA-Platform-Version頭) 所指定的。 在 Windows 10 及以上版本中,該數值是基於 Windows.Foundation.UniversalApiContract 版本。
要偵測特定版本的 Windows,請使用以下數值作為 platformVersion User-Agent 用戶端提示:
| 版本 | 第一版組件 platformVersion |
|---|---|
| Win7/8/8.1 | 0 |
| Win10 1507 | 1 |
| Win10 1511 | 2 |
| Win10 1607 | 3 |
| Win10 1703 | 4 |
| Win10 1709 | 5 |
| Win10 1803 | 6 |
| Win10 1809 | 7 |
| Win10 1903 | 8 |
| Win10 1909 | 8 |
| Win10 2004 | 10 |
| Win10 20H2 | 10 |
| Win10 21H1 | 10 |
| Win10 21H2 | 10 |
| Win11 | 13+ |