0

After many days of searching and testing I cannot solve this mystery of why a simple mobile redirect is not redirecting. I have tested several variations from across the web with no avail. If anyone has a moment to look at what I have, please do. Thanks in advance.

My goal: Redirect site to mobile site upon detection of user agent.

My code is below. NOTE: if I change

if(isMobile.any()) {

to

if(isMobile) {

the desktop browser will redirect the site.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script>
    var isMobile = {
Android: function() {
    return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
    return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
    return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
    return navigator.userAgent.match(/IEMobile/i);
    },
any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

if(isMobile.any()) {
   { window.location = 'http://www.briggsmechanical.com/mobile/index.html';
}
}
    </script>
Arnolfo
  • 1
  • 6
  • Any error messages for the mobile devices that did not work? – Spencer Wieczorek Jan 28 '15 at 17:53
  • 1
    The reason why it 'works' when you do if (isMobile) is because isMobile is an object and thus not falsey so the expression evaluates to true. – mbx-mbx Jan 28 '15 at 17:57
  • p.s. Mobile specific sites are not really necessary these days,with things like http://getbootstrap.com/ available you can just write a site that adapts to the device width. – mbx-mbx Jan 28 '15 at 17:58
  • I have been using "mobiletest.me" to check mobile so i'm not getting any error message. – Arnolfo Jan 28 '15 at 22:45
  • Magrangs- yes but I would expect the more specific (isMobile.any) would work. And yes, bootstrap is the way to go but this site is old html which is why I had to use the jscript and create a separate mobile site. – Arnolfo Jan 28 '15 at 22:56

1 Answers1

0

The code seemed to work for me.

I created this fiddle: http://jsfiddle.net/dkemvuyu/1/

But just replaced the 'if' with:

if(isMobile.any()) {
   alert('Device is a mobile');
} else{
   alert('Device is NOT a mobile');
}

and then navigated to it with my iphone and the alert came up as expected (telling me I was using a mobile device).

All I can assume is that the mobiletest.me website does not send the useragent header as part of the request that corresponds with the device to the website (p.s. I also tested navigating to the fiddle via the mobiletest.me website and it told me I was not a mobile device which confirms what I thought).

mbx-mbx
  • 1,765
  • 11
  • 23
  • Thank you Magrangs. Your assumption is correct in that the mobile testing sites do not read the useragent. That was completely my miss as I have just opened the site on a real mobile device and it all worked as expected. (shaking my head) – Arnolfo Jan 28 '15 at 23:34