JavaScript

How to detect a mobile device with JavaScript

In this tutorial, we are going to see different methods to detect a mobile device with JavaScript. Sometimes it is necessary to know whether our web page or website is running on a web browser on a mobile device. For verification, you can use one of the following methods. Both return “true” if the page is opened in a mobile device otherwise return “false”.
 

Method 1:
function mobilecheck() {
    return (typeof window.orientation !== "undefined") 
	    || (navigator.userAgent.indexOf('IEMobile') !== -1
	    );
};

 

 

Method 2:
function isMobileDevice() { 
 if( navigator.userAgent.match(/iPhone/i)
 || navigator.userAgent.match(/webOS/i)
 || navigator.userAgent.match(/Android/i)
 || navigator.userAgent.match(/iPad/i)
 || navigator.userAgent.match(/iPod/i)
 || navigator.userAgent.match(/BlackBerry/i)
 || navigator.userAgent.match(/Windows Phone/i)
 ){
    return true;
  }
 else {
    return false;
  }
}
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *