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