10

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

2
  • 2
    here, see this answer: stackoverflow.com/q/6850164/1291428 Commented May 27, 2012 at 21:56
  • 4
    the window size != the resolution of the screen. Commented May 27, 2012 at 21:56

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

screen.width and screen.height are returning incorrect values. Look at the observations below: - For iPad3 its returning 768 x 1024 where as its original resolution is 1536 x 2048. - For iPhone5 its returning 320 x 568 where as its original resolution is 640 x 1136. As per my observations, screen.width and screen.height are working perfectly fine for Android devices. But for iOS devices, its returning incorrect values. Any ideas for how to find Screen Resolution for iOS devices?
@AnkitPrajapati it returns the css resolution, which is the purpose of screen.width. You should always treat the device resolution as it's css resolution.
@AnkitPrajapati I know you've probably realised that by now anyway, but it's just in case anyone else has the same problem ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.