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 Upload a Project on Git
Suppose you are working in a big project...jQuery - Face Detect from Image
In this article, I will share with you h...Node.js MySQL Insert rows into Database Table
In this article, we will guide you to ho...Laravel 7 - Column sorting with pagination example
In this article, I will share with you h...Remove a Property from a JavaScript Object
Javascript object is pair of key/value....