Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jquery to determine screen width

Having trouble getting this script to run properly on an iphone 6. Keeps coming up as "not mobile". What am I missing?

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

EDIT: apologies, the code I typed here had a typo, but was not the cause for my issue. I had inaccurate info on iphone resolution. Thanks everyone!

like image 274
dsol828 Avatar asked Apr 26 '15 20:04

dsol828


People also ask

How does jQuery calculate window height?

Hope this helps. Basically, $(window). height() give you the maximum height inside of the browser window (viewport), and $(document). height() gives you the height of the document inside of the browser.

Which jQuery method is used to get or set the width of an HTML element?

css() is a method and width is a property name. It is one of the methods of jQuery dimension. It is used to get or set the width of the matched element. It is also used to get or set the width of the matched element.

What does innerWidth () method do in jQuery?

The innerWidth() method returns the inner width of the FIRST matched element.


1 Answers

The iPhone 6 display has a resolution of 1334x750. When emulating iPhone6 in chrome dev tools the width is reported as 980 (I don't know if this is accurate).

You might be interested in this: http://detectmobilebrowsers.com/

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

like image 88
ekuusela Avatar answered Sep 20 '22 11:09

ekuusela