2

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.

EDIT

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

SOLVED

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

SOLVED

Seanin
  • 131
  • 3
  • 12
  • You should checkout http://stackoverflow.com/questions/11381673/javascript-solution-to-detect-mobile-browser – Rob M. Apr 11 '13 at 14:28
  • Does scope of "handheld device" include all the possible handheld devices that are available? You can get close by following these guidelines: http://stackoverflow.com/questions/3514784/best-way-to-detect-handheld-device-in-jquery – Samuli Hakoniemi Apr 11 '13 at 14:29
  • 1
    Understand that this is a dangerous practice, because you may end up blocking devices that are perfectly capable of using your site. For example, not all Android devices are "mobile devices" -- Android can run on laptops, set-top boxes, etc. – apsillers Apr 11 '13 at 14:57
  • what would you suggest. BAring in mind i want to keep it extremly simple @apsillers – Seanin Apr 11 '13 at 14:59
  • See also http://stackoverflow.com/questions/743129/mobile-detection-using-javascript – Dagg Nabbit Apr 11 '13 at 15:02
  • I would suggest deciding for yourself what constitutes a "mobile device" (small screen size? touch support? low graphics performance?) and use feature detection to test for those properties on each device (for example, by using [Modernizr](http://modernizr.com/)). – apsillers Apr 11 '13 at 15:28

1 Answers1

1

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.

Community
  • 1
  • 1
strauberry
  • 4,083
  • 5
  • 30
  • 49