In today time, everyone wants to make responsive website. Some websites owner also wants to redirect mobile devices user to mobile website.
In this tutorial article, we will discuss few ways you can find device is mobile or computer. There are many ways you can detact device with Javascript.
Javascript provides simple way to get userAgent. By this way, you can check the current device.
var isMobile = false;
if( /Android|webOS|iPhone|iPad|Mac|Macintosh|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
isMobile = true;
}
The other one way is to find whether ontouchstart is available then it is likely to be a mobile device.
var isMobile = ('ontouchstart' in document.documentElement);
The third option is to check device width. Mostly device width less than 760px are mobile. If you only want to check screen width and serve user according to device width, then this is the perfect method. There is a JavaScript API built-in to detect media size.
var isMobile = window.matchMedia("only screen and (max-width: 760px)").matches;
or
if(screen.width <= 760) {
isMobile = true;
}
Though, there is no any 100% accurate method to find exact details, still the above methods are useful for it. If you have any method other than these, please let us know in below comment section.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to Build Multi Language App in Vue.js
This is a step by step Vue i18n tutorial...React CRUD Example using MongoDB | Express | React | NodeJs
Today, I am going to teach you how to&nb...Laravel Http Curl Request Example
cURL is a solid and simple tool that all...How to sort an associative array by value in PHP
Use the PHP asort() and arsort() functio...How to install Oracle VirtualBox in Ubuntu
VirtualBox is the free and open-source s...