开发者

Desktop/OS User Agents in Javascript

开发者 https://www.devze.com 2023-01-08 04:19 出处:网络
Below is my current script: <script type=\"text/javascript\"> if (navigator.userAgent.indexOf(\'BlackBerry\')!= -1){

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 eit开发者_JAVA技巧her the OS IDs or the browser's IDs (i .e firefox, chrome, IE, opera).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消