Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get iPad screen width in Javascript

I need to dynamically get the screen size of all mobile devices from a webpage using Javascript.

I have tried this:

//get window's size
if (document.body && document.body.offsetWidth) {
 windowsWidth = document.body.offsetWidth;
 windowsHeight = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
    document.documentElement &&
    document.documentElement.offsetWidth ) {
 windowsWidth = document.documentElement.offsetWidth;
 windowsHeight = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
 windowsWidth = window.innerWidth;
 windowsHeight = window.innerHeight;
}

But on iPad I get this size: 980 x 1080 (not the real 768 x 1024).

Thanks

Mauro

like image 772
Mauro Cattaneo Avatar asked May 27 '12 21:05

Mauro Cattaneo


1 Answers

Getting the screen dimensions on iPad requires reading the width and height properties of the screen object.

var height = screen.height;
var width = screen.width;

These will provide 768 and 1024 depending on orientation.

like image 167
Jacob T. Nielsen Avatar answered Sep 17 '22 15:09

Jacob T. Nielsen