Ad

How Do I Get What Kind Of Device Who Visits My Website?

- 1 answer

I am making a JavaScript game but I want the canvas to be different sizes depending if it is for instance an iPhone who visits

const canvas = document.querySelector("canvas"); canvas.width = 500;

So my question is how do I get the device information?

Ad

Answer

What you are looking for is window.navigator.platform.

switch(window.navigator.platform) {
   case 'iPhone':
      // Set width for iPhones
      break;
   case 'Android':
      // Set width for Androids
      break;
   default:
      // Set width of other devices
      break;
}
Ad
source: stackoverflow.com
Ad