Using jquery to determine screen width

You are having trouble getting this script to work correctly on iphone 6. Saved as โ€œnot mobileโ€. What am I missing?

$(document).ready(function(){ if ($(window).width < 700){ alert("mobile"); } else { alert("not mobile"); } }); 

EDIT: Sorry, the code I printed here had a typo, but was not the cause of my problem. I had inaccurate iphone resolution information. Thanks everyone!

+5
source share
6 answers

The iPhone 6 has a resolution of 1334x750. When emulating iPhone6 โ€‹โ€‹in chrome dev tools, the width is declared as 980 (I don't know how accurate it is).

You may be interested in: http://detectmobilebrowsers.com/

Also, as others have noted, replace $(window).width with $(window).width()

+7
source

iPhone6 โ€‹โ€‹- 1334x750 pixels. If you use only the width to detect the mobile user, see this .

+3
source

Well, ignoring what ekuusela said about screen resolution, you seem to have forgotten your parentheses after width , which is a method, not a field. To fix this, simply add () after it:

 if ($(window).width() < 700) 

See the documentation for width() for more details.

+2
source

JQuery uses $(window).width() . This is a function, not a property.

+1
source

You need .width() , not just .width . Also, write it down and make sure you expect it.

0
source

This thread penetrates deeply into parameters in both Javascript and jQuery.

Get screen size, current web page and browser window

0
source

All Articles