views:

1173

answers:

3

Has anyone noticed this behavior? I'm trying to write a script that will trigger upon a resize. It works fine on normal browsers, works fine on iPhone, but on iPad, will only trigger going from horizontal to vertical viewport, not vice versa.

Here's the code:

$(window).resize( function() {

    var agent=navigator.userAgent.toLowerCase();
    var is_iphone = ((agent.indexOf('iphone') != -1));
    var is_ipad = ((agent.indexOf('ipad') != -1));

    if(is_iphone || is_ipad){
        location.reload(true);
    } else {     
        /* Do stuff. */
    };
});
A: 

The only thing I could find from apple:

Safari on iPad and Safari on iPhone do not have resizable windows. In Safari on iPhone and iPad, the window size is set to the size of the screen (minus Safari user interface controls), and cannot be changed by the user. To move around a webpage, the user changes the zoom level and position of the viewport as they double tap or pinch to zoom in or out, or by touching and dragging to pan the page. As a user changes the zoom level and position of the viewport they are doing so within a viewable content area of fixed size (that is, the window). This means that webpage elements that have their position "fixed" to the viewport can end up outside the viewable content area, offscreen.

I understand the "works on the iPhone" part...but maybe it doesn't anymore? This could be a change in OS/mobile Safari since the latest public iPhone OS release shipped (the above documentation is from March 2010).

I'm going to re-tag this question adding iPhone to it, maybe one of the guys with the developer 4.0 OS release can test this? If it is the case it's been removed, this should be a bug filed/fixed before it goes live...I'm not sure on how the procedures are on this with Apple are though.

Nick Craver
Thanks for looking it up and adding the tag. To compound my confusion, I'm no Javascript zen master, so I wasn't even sure if I was putting the code together correctly.
dclowd9901
@dclowd9901 - The code looks alright, just an extra `;` after the `if` there :) Just curious, why do you need to reload when the page resizes?
Nick Craver
I have functionality in a slideshow plugin that I wrote that gives it fullscreen viewing with a parameter. Users brought it to my attention that lack of accommodating window resizes was an oversight. I tried to write a bandaid before I release a new tested version, but came across the issue about which I posted.
dclowd9901
+3  A: 

I think what you want would be to use the iPad Orientation CSS, which looks like this:

<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css" />
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css" />

Also, the orientationchange event fires when the orientation is changed, according to iPad web development tips.

Together, that should give you tools enough to deal with the change.

artlung
I can't test this yet, but this is probably exactly what I'm looking for. Great general resource for general iPad development too. Thanks in advance artlung! I'm looking forward to trying this out!
dclowd9901
Cool! Hope it's helpful. I have a friend who just got one and am eager to play with it.
artlung
This doesn't seem to be working. The safari browser doesn't seem to recognize the `orientationchange` property.
dclowd9901
I don't have an iPad or iPhone to test with. Is that where you're testing?
artlung
Yep. I have both.
dclowd9901
I should mention the browser built in debug outright says "window.orientationchange" is not a function.
dclowd9901
@dclowd: `window.`**`on`**`orientationchange`.
KennyTM
@KennyTM: Looks like we have a winner. Put it up as an answer, and collect your prize ;)
dclowd9901
+1  A: 

If I understood you correctly, you want to do something when the user tilts the iPad. Here you go:

window.onorientationchange = function(){

    var orientation = window.orientation;

    // Look at the value of window.orientation:

    if (orientation === 0){

        // iPad is in Portrait mode.

    }

    else if (orientation === 90){

        // iPad is in Landscape mode. The screen is turned to the left.

    }


    else if (orientation === -90){

        // iPad is in Landscape mode. The screen is turned to the right.

    }

}

The left picture shows portrait mode, the right one landscape mode

Vincent
I chose your answer because it was most concisely the correct answer. As a point of notation, though, the solution I went with incorporated none of the `if` checking. It simply ran code (namely a page reload) when the orientation was changed.
dclowd9901
@dclowd9901 Yes, I thought so, I just wanted to provide some extra information :)
Vincent