admin管理员组

文章数量:1391811

I'm trying to alert mobile device users on my site to use their devices in landscape mode. I have an alert pop up but it seems to be getting caught in an infinite loop. How would I go about stopping that loop after the user has clicked ok?

Here is the code:

window.ondeviceorientation = detectIPadOrientation;
function detectIPadOrientation () 
{
    if ( orientation == 0 || orientation == 180 ){
        alert ('Please use your iPad in landscape mode'); 
    }
}

I'm trying to alert mobile device users on my site to use their devices in landscape mode. I have an alert pop up but it seems to be getting caught in an infinite loop. How would I go about stopping that loop after the user has clicked ok?

Here is the code:

window.ondeviceorientation = detectIPadOrientation;
function detectIPadOrientation () 
{
    if ( orientation == 0 || orientation == 180 ){
        alert ('Please use your iPad in landscape mode'); 
    }
}
Share Improve this question edited May 20, 2013 at 16:02 Pointy 414k62 gold badges595 silver badges629 bronze badges asked May 20, 2013 at 15:58 Allan MoodyAllan Moody 311 gold badge1 silver badge5 bronze badges 3
  • 2 FWIW, telling people what to do with their device isn't exactly a UX "best practice." – Pointy Commented May 20, 2013 at 16:03
  • It's only for a specific page not for the entire site. @Pointy – Allan Moody Commented May 20, 2013 at 16:08
  • Perhaps this has to do with how quickly iOS rechecks screen position - over and over I presume. You could set a flag that would ensure it only pops up one time. var pop=true; if(orientation... && pop){alert(...); pop=false};} – rGil Commented May 20, 2013 at 16:11
Add a ment  | 

2 Answers 2

Reset to default 2

The following code will send an alert based on screen orientation. The alert will only be sent if the screen is in landscape mode.

window.onload = function() {
    if ( window.orientation == 0 || window.orientation == 180 ) { 
        alert ('Please use your mobile device in landscape mode'); 
    }
};

You can make the website alert users who are using a mobile device, not just when the device is in landscape.

This question will probably help you out.


Or, the answer to your exact question might be on this duplicate question.


You may also try identifying the the user with

var isiPad = navigator.userAgent.match(/iPad/i) != null;

And sending him an alert if this does not equal NULL

I hope this helped you out.

本文标签: Javascript Alert on mobile devices based orientationStack Overflow