我可以使用javascript来检测移动设备并限制对正常浏览器的访问吗?

Can I use javascript to detect a mobile device and restrict access to a normal browser?

本文关键字:浏览器 访问 javascript 可以使 检测 移动 我可以      更新时间:2023-09-26

我希望我的网页检测用户是否使用手持设备,如果是,允许访问移动网站,如果没有重定向到主站。我假设这将在JavaScript中完成。有谁能帮助我或给我指个方向吗?

编辑

我仍然想使用javascript,因为它是我在整个项目中使用的语言。

解决
<script language="javascript"> 
 var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows'sce|palm/i.test(navigator.userAgent.toLowerCase()));
          if (mobile) {
              window.location.replace("http://scmweb.infj.
              ulst.ac.uk/~b00519427/finalwebsite/homepage.html");
          }
          else
            window.location.replace("http://scm.ulster.ac.uk/");
   </script>
解决

要真正执行一个特定的站点,JS不是正确的选择,因为它很容易被禁用。因此,我建议服务器端决策和执行。为此,您可以使用.htaccess文件和USER_AGENT规则,如本回答所述。