How to detect mobile device and redirect with JavaScript

Increasing the use of mobile devices like smartphones and tablets, it is essential for every website owner to give the mobile experience of their website to the users. For example we detect mobile device or browser and redirect our visitor to the specific landing page where you provide your smartphone app link. So the user can install your mobile app. 

detect mobile device and redirect

Code to detect mobile device and redirect

First, we open index.html file in any text editor.

Second, in the head section <head> add small JavaScript code.

<script language="javascript"> 
  if(!window.location.search.substring(1) == "full=true") {  
    if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/Android/i)) { // detect mobile browser
      window.location.replace("http://example.com/mobile-page.html");  
    }
  }
</script>

How this code work

It search and match browser like Android, iPhone, iPad and other mobile browser and send to mobile specific page.

I've loved ❤ technology and always curious about new tech innovation. I've been a technology writer and passionate blogger for last 3 year and write How to guide to help people become a tech-savvy.

Leave a Comment