launchoverit
launchoverit

Reputation: 5207

Getting inconsistent results from window.innerWidth in Mobile Safari

I'm using this javascript to detect the browser width:

$(document).ready(function(){ var width = window.innerWidth; });

It appears to be giving inconsistent results. I've made a working codepen here. by repeatedly refreshing the full-page view in mobile Safari I've received both of the following results:

ios9 Mobile Safari window.innerWidth ios9 Mobile Safari window.innerWidth

It jumps back and forth inconsistently, and appears to be exacerbated by a larger page size (hence all the images). Sometimes it will only return one result until I kill the app and re-open it.

Is there a better way to get the width of the browser?

Notes:

Thanks!

Upvotes: 1

Views: 1181

Answers (1)

adzza24
adzza24

Reputation: 2084

Safari gets inconsistent widths in vanilla javascript because of the resolution. JQuery usually standardised this. Try using:

$(window).width()

This should give you the right width.

Upvotes: 1

Related Questions