简体   繁体   English

简单的JavaScript检测用户的设备

[英]Simple javascript detect user's device

I want to add javascript into my template to detect that every time users view my websites with what kind of device (Smartphone, tablet or PC). 我想在模板中添加JavaScript,以检测用户每次使用哪种设备(智能手机,平板电脑或PC)查看我的网站。

Currently, I try to do it with javascript, here it is: 目前,我尝试使用javascript进行操作,如下所示:

var smartPhone = "smartphone";
var Ipod = "ipod";

var userDevice = navigator.userAgent.toLowerCase();

function DetectPhone()
{
   if (userDevice.search(smartPhone) > -1)
      return true;
   else
      windows.location = "/pageforphone.aspx";
}

function DetectIpod()
{
   if (userDevice.search(Ipod) > -1)
      return true;
   else
      windows.location = "/pageforpod.aspx";
}

Did I write the codes right? 我写的代码正确吗? Honestly, no iPhone... 老实说,没有iPhone ...

Of course, if these were completed with Php or Asp.net would be better, but is it true that people always turn the javascript off on their palm devices? 当然,如果这些都用Php或Asp.net完成会更好,但是人们总是在其Palm设备上关闭javascript吗? Also, i think iphone or ipod or any other tablets are quite similar, is it ok that i use same redev pages for those devices? 另外,我认为iphone或ipod或任何其他平板电脑都非常相似,可以为这些设备使用相同的redev页面吗? Actually i am not very understanding what is the differences among them and what should i be aware when i redev my web pages. 实际上,我不太了解它们之间的区别以及重新定义网页时应注意的事项。

The premise of the question, that browser-sniffing is acceptable, is false. 这个问题的前提是浏览器嗅探是可以接受的,这是错误的。 You should never engage in browser sniffing, simply because you cannot confidently accomplish what you set out to accomplish. 绝对不要因为无法自信地完成您打算完成的工作而进行浏览器嗅探。 User Agent Strings are not immutable, and are often times changed by plugins installed on the client machine, or by the client themselves (not uncommon, since bad programmers may lock them out of a site based on their UA-String). 用户代理字符串不是一成不变的,并且经常通过客户端计算机上安装的插件或客户端本身进行更改(这并不罕见,因为不良的程序员可能会基于其UA-String将其锁定在站点之外)。

Rather than asking what device the user is one, you should instead be asking what functionality is the device capable of. 与其问用户是哪台设备,不如问用户该设备具备什么功能 This brings you to feature detection , where you set out to do X, test to see if the user agent is capable of doing X, and then proceed to do or not do X. For instance: 这将带您进行功能检测 ,在这里您开始执行X,测试以查看用户代理是否能够执行X,然后继续执行X。例如:

if ( !!document.createElement("video").canPlayType ) {
  console.log( "HTML5 Video Supported" );
}

This is more reliable than trying to see if the user is on an iPhone, iPad, Windows Tablet, or HTML5 Enabled Browser. 这比尝试查看用户是否在iPhone,iPad,Windows Tablet或启用HTML5的浏览器上更为可靠。 You can use tools like Modernizr to perform a lot of these tests for you - over 40 in fact. 您可以使用Modernizr之类的工具为您执行许多此类测试-实际上超过40种。

There many mobile devices on the market + each device can have more than one browser installed. 市场上有许多移动设备+每个设备可以安装多个浏览器。 I would prefer using some library rather than funding "iPhone" in user agent string (I'm using Android phone with Opera browser myself) 我宁愿使用一些库,也不希望在用户代理字符串中为“ iPhone”提供资金(我自己将Android手机与Opera浏览器一起使用)

For example: 例如:

Both of them are regularly updated with latest mobile devices released and provide you with information which features are supported on given device 两者都会定期更新,并会发布最新的移动设备,并为您提供给定设备支持哪些功能的信息

This is how I use javascript to detect iOS devices 这就是我使用JavaScript检测iOS设备的方式

<script type="text/javascript">
    var agent = navigator.userAgent.toLowerCase();
    var isiPhone = ((agent.indexOf('iphone'))!=-1)

    if (((agent.indexOf('iphone'))!=-1) {
        document.location = "mobile.aspx?device=iphone";
    }

    if (((agent.indexOf('ipod'))!=-1) {
        document.location = "mobile.aspx?device=ipod";
    }

    if (((agent.indexOf('iPad'))!=-1) {
        document.location = "mobile.aspx?device=ipad";
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM