I have a site that I want to behave slightly differently on a computer than on a mobile device (phone or tablet) and I want a function that I can call to figure out what type of device I'm on. Is there like window.getDevice or document.getDevice or something like that?
Asked
Active
Viewed 337 times
0
-
1What exactly is the behavior that will differ between mobile and desktop? That will inform how to answer this question. – ldtcoop Jul 08 '19 at 22:17
-
Take a look at this SO answer: https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device . There are shown some techniques per user-agent and a hacky method that uses a portion of CSS (I really like that one). – Torsten Barthel Jul 08 '19 at 22:20
-
2Possible duplicate of [What is the best way to detect a mobile device?](https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device) – Jonathan Jul 09 '19 at 00:03
2 Answers
1
I think that there isn't a way to get the device name.
You can use:
window.navigator.userAgent to get info about platform in use and browser.
and
window.screen to get an object with available width and available height that tell you the real width and height of device in use.

Daniele Tulone
- 46
- 1
- 2
0
I have a suggestion, use this website's api https://www.iplocinfo.com/, it will help you to figure out the visitor information (the browser type with version, device type and version, mobile, desktop, tablet or bot .... and the ip address and location and privacy in the same time): example:
const getApiData = async () => {
let ip_address = "155.45.44.44";
let api_key = "uicVcaMqPM_zumdStwSQrA"; //Add your api for testing
let user_agent = "Mozilla/5.0 (Linux; Android 12; SM-S906N Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/80.0.3987.119 Mobile Safari/537.36";
let endPoint = `https://www.iplocinfo.com/api/v1/${ip_address}?apiKey=${api_key}&user-agent=${user_agent}`
let response = await fetch(endPoint, {
method: 'GET',
headers: {
"Content-type": "application/json",
},
})
let data = await response.json();
console.log(data);
};
getApiData();
and you can get the ip address easy by fetch it from this api example:
const getIpAddress = async () => {
let endPoint = `https://www.iplocinfo.com/ip`;
let response = await fetch(endPoint, {
method: 'GET',
headers: {
"Content-type": "application/json",
},
})
let data = await response.json();
console.log(data);
};
getIpAddress();

Solve
- 11
- 2