JSFiddle - React, Tailwind, and code Playground
JavaScript
function detectDevice(s) {
var userAgent = navigator.userAgent.toLowerCase(),
platform = navigator.platform.toLowerCase();
if (s) {
if (userAgent.match(s.toLowerCase()) != null)
return true;
return false;
}
this.isSmall = function() {
if (screen.width <= 480 && screen.height <= 480)
return true;
return false;
};
this.device = function() {
var a = ["Android", "iPhone", "iPad", "iPod", "BB", "webOS"];
for (var i = 0; i < a.length; i++) {
if (userAgent.match(a[i].toLowerCase()) != null)
return a[i];
}
var a = ["Win", "Mac", "Linux"];
for (var i = 0; i < a.length; i++) {
if (platform.match(a[i].toLowerCase()) != null)
return (a[i] == "Win") ? "Windows" : a[i];
}
return "Unknown Device";
};
this.userAgent = userAgent;
this.platform = platform;
}
var device = new detectDevice();
document.body.innerHTML += "Is small: " + device.isSmall() + "<br />";
document.body.innerHTML += "Device: " + device.device() + "<br />";
document.body.innerHTML += "Is iPad: " + detectDevice("iPad") + "<br />";
document.body.innerHTML += "Is Android: " + detectDevice("Android") + "<br />";
document.body.innerHTML += "Is Linux: " + detectDevice("Linux") + "<br />";
document.body.innerHTML += "Is Windows: " + detectDevice("Windows") + "<br />";
console.log(device.userAgent, device.platform);