[英]What is the reliable way to detect OS using Javascript?
The use of navigator.userAgent is not advised as per MDN.根据 MDN,不建议使用navigator.userAgent 。 In my application I want to have a shortcut for del key.
在我的应用程序中,我想要一个 del 键的快捷方式。
In mac del key (is backspace) with charCode = 8. In windows del key charCode = 46.在 mac del 键(是退格键)中,charCode = 8。在 windows del 键中,charCode = 46。
I want to treat both keyEvents same.我想对待两个 keyEvents 相同。 I am currently using userAgent but it is unreliable as that property can be spoofed.
我目前正在使用 userAgent,但它不可靠,因为该属性可以被欺骗。 I am wondering what is the best way to know the client OS otherwise.
我想知道否则了解客户端操作系统的最佳方式是什么。
More deprecated navigator properties navigator.appVersion navigator.platform更多弃用的导航器属性navigator.appVersion navigator.platform
/* code objective */
if ((keycode == 8 && os == 'macintosh') || keycode == 46) {
//This is keyboard shortcut to perform delete
}
Relying on KeyboardEvent.metaKey and KeyboardEvent.Backspace solved my problem依靠 KeyboardEvent.metaKey 和 KeyboardEvent.Backspace 解决了我的问题
Full Key Values - https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values完整的键值 - https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
For more details refer: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values有关更多详细信息,请参阅: https : //developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Key_values
Thanks to @Teoli2003 for twitter reply - https://twitter.com/Teoli2003/status/663995373766221824感谢@Teoli2003 的 twitter 回复 - https://twitter.com/Teoli2003/status/663995373766221824
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.