-2

I wrote some simple code to find the type of the device:

$(document).ready(detectDevice);

function detectDevice() {
    if (window.innerWidth > 768) {
        var device = "Desktop";

        console.log('=======device======', device);
    } else if (window.innerWidth < 767 && window.innerWidth > 481) {
        var device = "iPad";
    } else if (window.innerWidth < 480) {
        var device = "Mobile Phone";
    }
}

But I want to find the correct model name of the device from which the app is being used. I don't want to write the code using navigator.userAgent which will find out using browsers.

Please can someone tell me if there is any way to find this using JavaScript or jQuery?

Toothbrush
  • 2,080
  • 24
  • 33
  • 1
    You don't want to use navigator.userAgent then what you want exactly? – Bhojendra Rauniyar Aug 14 '14 at 10:37
  • possible duplicate of [How to detect mobile device and get user agent info send and save that information to database on server, only once?](http://stackoverflow.com/questions/6081346/how-to-detect-mobile-device-and-get-user-agent-info-send-and-save-that-informati) – hjpotter92 Aug 14 '14 at 10:37
  • Check out [this](http://stackoverflow.com/questions/7400489/ipad-version-detection-in-javascript) for iPads. – Umut Seven Aug 14 '14 at 10:41
  • navigator.userAgent will findout the device based on browser right. But I wont run my application on browser. So, I want some other way to find the device. – Srilakshmi B S Aug 14 '14 at 10:42
  • https://github.com/matthewhudson/device.js this small client script does that just for you - check it out. – MartinWebb Aug 14 '14 at 10:52
  • No. That is based on mobile browser @hjpotter92 – Srilakshmi B S Aug 14 '14 at 11:04
  • Can you please explain how can i make use of device.js @MartinWebb – Srilakshmi B S Aug 14 '14 at 12:42
  • If you check the link the script comes with clear instructions, in your case you can look at the code and it shows you exactly how the programmer identifies the devices, the script may fit your needs or it may give you the info you need to make your own. – MartinWebb Aug 14 '14 at 18:37

1 Answers1

0

You can use this lightweight libary to do this

https://github.com/matthewhudson/current-device

Here is how it works:

  console.log("device.portrait() === %s", device.portrait());
  console.log("device.landscape() === %s", device.landscape());
  console.log("device.mobile() === %s", device.mobile());
  console.log("device.tablet() === %s", device.tablet());
  console.log("device.ipad() === %s", device.ipad());
  console.log("device.ipod() === %s", device.ipod());
  console.log("device.iphone() === %s", device.iphone());
  console.log("device.android() === %s", device.android());
  console.log("device.androidTablet() === %s", device.androidTablet());
  console.log("device.blackberryTablet() === %s", device.blackberryTablet());
  console.log("device.fxos() === %s", device.fxos());
  console.log("device.fxosPhone() === %s", device.fxosPhone());
  console.log("device.fxosTablet() === %s", device.fxosTablet());

Test for Ipad

  if (device.ipad()) //....do something
Dave S
  • 788
  • 1
  • 11
  • 30
MartinWebb
  • 1,998
  • 1
  • 13
  • 15
  • This seems hard to maintain. There are so many devices in the world; how could you even track them all? – Bradyo Jan 20 '21 at 21:19