views:

993

answers:

2

Out of curiosity I've been playing with jQuery to determine the browser's screen size, and it occurred to me that screen size could be used to determine whether or not a visitor was using an iPhone/iTouch to view the site.

So I used the following to test this:

$(document).ready(

    function() {

        var screenX = screen.width,
            screenY = screen.height;

        alert("X: " + screenX + " Y: " + screenY);

        if (screenX == 320 && screenY == 396) {
            $('div#wrap').css('background-color','#f00');
        }

        else if (screenY == 320 && screenX == 396) {
            $('div#wrap').css('background-color','#0f0');
        }
    }
);

On viewing the page via iPhone, I notice that the dimensions are consistently (regardless of orientation):

x: 320, y: 396

This is regardless of orientation. I haven't, as yet, attempted to use an onChange event to detect changes (mainly because I'm still so new at jQuery), but I wondered if there was a way to determine, via jQuery or plain javascript, the iPhone/iTouch's orientation?

+4  A: 

window.orientation will give you an integer that denotes the rotation. You can listen for orientation changes by adding an event to the body:

<body onorientationchange="updateOrientation();">

Edited by OP, just on the off-chance that the link dies or gets moved at some point...

Value  |  Description
-------+-------------------------------------------------------------------------------
 0     |  Portrait orientation. This is the default value.
-90    |  Landscape orientation with the screen turned clockwise.
 90    |  Landscape orientation with the screen turned counterclockwise.
 180   |  Portrait orientation with the screen turned upside down. This value is currently not supported on iPhone.
Typeoneerror
That's awesome, thanks! =)
David Thomas
(Oh, and I hope you don't mind, but I edited your post just to bring the relevant parts of the document inline.)
David Thomas
Don't mind at all! That's kinda the point of the site ;) Can I ask what you're doing with this type of function? A mobile view of something existing or something brand new? Just curious.
Typeoneerror
At the minute I'm just playing with the possibilities (giggling to myself as the alerts update... =p), but I figure that the consistency of the `x` and `y` (coupled with the presence of `window.orientation`) gives a pretty good indication that the viewer is using an iPhone/iPod Touch. So I think it'll be useful, but I've got no use-case for it, yet. Still, it's a *nice-to-have*, I think... =)
David Thomas
Seems like this is the wrong place to put user agent detection, you should detect on the server side and serve appropriate content
Michael Mullany
Does window.orientation change to reflect the user turning their device? I would test it myself, but lack an iPhone and turning my laptop on it's side isn't working </joke>.
ehdv
@ehdv it does, you just have to listen to orientation change events with "onorientationchange"
Typeoneerror
@Michael Mullany, I agree that this would be the wrong place for browser detection, but I can see a use for it, perhaps, serving to adapt the layout subtly, for small client-side tweaks maybe. It might be useful, knowledge is generally a good thing.
David Thomas