Desktop/OS User Agents in Javascript

旧巷老猫 提交于 2019-12-20 07:15:19

问题


Below is my current script:

<script type="text/javascript">
if (navigator.userAgent.indexOf('BlackBerry')!= -1){
    document.write('<link rel="stylesheet" href="/m/css/mobile.css" type="text/css" />');
} else if (navigator.userAgent.indexOf('iPhone')!= -1) {
    document.write('<link rel="stylesheet" href="/m/css/smartmobile.css" type="text/css" />');
}else if (navigator.userAgent.indexOf('Android')!= -1) {
    document.write('<link rel="stylesheet" href="/m/css/smartmobile.css" type="text/css" />');
} else if (navigator.userAgent.indexOf('Windows XP')!= -1) {
    document.write('<link rel="stylesheet" href="/m/css/smartmobile.css" type="text/css" />');
}
else{
    document.write('<link rel="stylesheet" href="/m/css/mobile.css" type="text/css" />');
}
</script>

The Windows XP is not working, and it's defaulting to the last else clause on a desktop computer. I need either the OS IDs or the browser's IDs (i .e firefox, chrome, IE, opera).


回答1:


Your "Windows XP" line is not working as Windows XP identifies itself as "Windows NT 5.1".

However, I'm curious as to why you'd want to do this at all. UserAgent sniffing in any form is generally frowned upon in the web-development community - there's always a far better (usually far easier) way.



来源:https://stackoverflow.com/questions/3268740/desktop-os-user-agents-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!