我可以使用JavaScript来检测移动设备并限制对普通浏览器的访问吗? [英] Can I use javascript to detect a mobile device and restrict access to a normal browser?

查看:83
本文介绍了我可以使用JavaScript来检测移动设备并限制对普通浏览器的访问吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的网页检测用户是否正在使用手持设备,如果允许,则允许访问移动站点,如果不允许,则重定向到主站点.我假设这将在JavaScript中完成.谁能帮助我或指出正确的方向.

I want my web page to detect whether the user is using a handheld device and if so allow access to mobile site and if not redirect to main site. I am assuming this would be done in JavaScript. Can anyone help me or point me in the right direction.

编辑

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

I would still like to use javascript as it is the language I have used throughout the project.

已解决

<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规则,如此答案中所述

To really enforce a particular site, JS is not the right choice since it can easily be disabled. Hence, I would propose a server side decision and enforcement. For this, you can use a .htaccess file and the USER_AGENT rule, as described in this answer.

这篇关于我可以使用JavaScript来检测移动设备并限制对普通浏览器的访问吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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