This problem is complicated.
You can find out the screen resolution of your device in JavaScript using screen.width and screen.height, just like on the desktop.
However, on a mobile device, 1 CSS pixel will not necessarily have a 1: 1 aspect ratio with a physical screen pixel. Say your screen.width returns a value of 640 pixels. If you add a banner with a width of only 640 pixels, it will most likely go over the available screen width of your phone.
The reason for this is because mobile device manufacturers are setting the default view for the device for a different scale factor than the screen of your device. Thus, the contents of web pages that were not specifically designed for mobile devices will still be displayed without the need for scaling.
If you use the meta viewport tag, you can set the zoom factor of your viewport to 1: 1 by setting its width value to the width of the device, for example:
<meta name="viewport" width="device-width" content="target-densitydpi=device-dpi" />
Now that the 640-pixel banner will exactly match your 640-pixel screen.
Unfortunately, from this point on, as far as I know, there is no real way to tell the actual size of the physical screen (in inches or mm) of the device that displays your web content. At least not in JavaScript. Although the browser can access some useful information, such as device.name or device.platform, you cannot determine how many inches or mm these 640 pixels represent if you do not know the screen measurements of this device in advance.
I understand very well why someone will need a version of the application for small phone screens and even larger tablet screens. For example, small buttons. Sometimes they are so close together that your finger will span two at once. At the same time, you donโt need these thick buttons designed for mobile screens, showing on a good 10-inch tablet.
You could, of course, with the help of screen.width and window.devicePixelRatio find out if you work, say, on the retina of the iPhone or iPad 2, but when you start to consider the many screen resolutions and pixel densities on Android devices, the task becomes almost impossible.
The solution goes Native.
On Android, you can use the code in the following thread to find out if your application is running on the big screen:
tablet or phone - Android
You can then use the code in this other thread to request your shell application from JavaScript using PhoneGap:
The relationship between Android Java and Phonegap Javascript?
With Apple, this is even more straightforward:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
And if you use the same naming convention for your Android and Apple objects, your code will still be universal, and thatโs the whole point of PhoneGap. For example, in my case, I used the same names for both Java and my Objective-C objects and methods. In JavaScript, I just need to call:
window.shellApp.isTabletDevice();
And my code works and looks beautiful on every screen size.