As per MDN, the recommended method is to utilize navigator.userAgentData.platform
, however, the drawback of this approach lies in its lack of support from FireFox, Safari, Android Browser, or Chrome on Android.
An alternative suggestion to the [navigator.platform] property is to use navigator.userAgentData.platform
. Nevertheless, some major browsers have not yet implemented support for navigator.userAgentData.platform
, and the specific definition remains unrecognized by any standards group (specifically, it does not form part of any published specification by the W3C or WHATWG).
For now at least, opting for navigator.userAgent
appears to be a reasonable choice. It currently enjoys support from all leading browsers; nonetheless, browser manufacturers may modify the content provided without prior notice, thus necessitating caution.
The specification urges browsers to limit the information disclosed through this field. It is unwise to presume that the value attached to this property will remain constant across future browser versions. Ideally, refrain from using it altogether, or restrict usage to current and past browser versions. There exists a likelihood of new browsers adopting the same UA, or parts thereof, as an older browser - casting doubt on the authenticity of the browser agent portrayed through this property.
Further, bear in mind that users hold the liberty to alter the contents of this field if desired (UA spoofing).
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/userAgent
Given these considerations, a potential solution could involve:
const os = (() => {
if (/windows/i.test(navigator.userAgent)) {
return "Windows"
}
else if (/iphone/i.test(navigator.userAgent)) {
return "iOS"
}
else if (/ipad/i.test(navigator.userAgent)) {
return "iOS"
}
else if (/macintosh/i.test(navigator.userAgent)) {
return "Mac OS"
}
// add more user agents to detect...
})();