Javascript 中的桌面/操作系统用户代理 [英] Desktop/OS User Agents in Javascript

查看:18
本文介绍了Javascript 中的桌面/操作系统用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我当前的脚本:

<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>

Windows XP 不工作,它默认为台式计算机上的最后一个 else 子句.我需要操作系统 ID 或浏览器 ID(即 firefox、chrome、IE、opera).

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).

推荐答案

您的Windows XP"行不工作,因为 Windows XP 将自身标识为Windows NT 5.1".

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

但是,我很好奇您为什么要这样做.网络开发社区通常不赞成任何形式的 UserAgent 嗅探 - 总有更好(通常更简单)的方法.

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.

这篇关于Javascript 中的桌面/操作系统用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆