in5 Answers archive · asked 2017-10-26 · 1 answer

device detection code desktop vs mobile

desktopmobiledetectcode

What I want to do is create a file for web and one as a web app (for use on mobile) and add code to each that will detect what device the user is viewing it on. If the user is viewing the web link on a mobile device I want it to detect that and redirect them to the web app (mobile version). If the user is viewing the mobile link from a desktop I want it to detect that and redirect them to the web version. Now i'm no coder so I've googled this and found a few options that didn't work like:

<script type="text/javascript">
if (screen.width >= 699) {
window.location = "http://www.jayack.com/desktop/";
}
</script>
 
or
 
<script type="text/javascript">
if((navigator.userAgent.match(/iPhone/i)) || 
 (navigator.userAgent.match(/iPod/i))) {
   if (document.cookie.indexOf("iphone_redirect=false") == -1) window.location = "http://www.jayack.com/mobile/";
}
</script>
 
Has anyone done this before? Can anyone please point me in the right direction or assist that would be much appreciated. Thanks in advance  

 

in5 user · 2017-10-26 · 2,992 views

1 answer

Accepted answer

I figured it out, first off I did not add any code within the in5 export window. I exported normally and did not edit the code it exported. I used the method presented on http://detectmobilebrowsers.mobi I added their mobile_device_detect.php file on the same level as the index.html along with a second php file with the following code in it

<?php
 
// http://detectmobilebrowsers.mobi/
 
require_once('mobile_device_detect.php');
$mobile = mobile_device_detect();
 
// redirect all mobiles to mobile site and all other browsers to desktop site
if($mobile==true){
  header('Location:http://yourmobilesite.com/');
}else{
  header('Location:http://yourdesktopsite.com/');
}
exit;
 
?>
 
So now when a user has the desktop link and is viewing it from a mobile device it will automatically redirect them to web app build. If someone has the web app link and tries to view it from a desktop it automatically redirects them to the regular web build. 
 
Any questions let me know 

 

in5 user · 2017-10-27

Nice! Glad you found a solution that works for you. :-)

in5 team · 2017-10-27

Was this useful?