I have some code I wrote that alerts the user on mobile phones that change to landscape to basically go to portrait. This works great but if the user starts off in landscape it obviously doesnt show since there is no orientation change. Is there a way to just get the orientation of the get go?
$(window).on("orientationchange",function(){
if( (window.orientation == 90 && $( window ).width() < 780) || (window.orientation == -90 && $( window ).width() < 780) ) // Landscape
{
$('body').append('<div class="phoneLandscape"><h4>Sorry, this site is best viewed in Portrait Mode</h4></div>');
}
else // Portrait
{
$('.phoneLandscape').remove();
}
});
jQuery mobile has the method $(window).on("orientationchange")
but if you're looking to detect orientation at any given time try this
if(window.innerHeight > window.innerWidth){
//portrait
}
if(window.innerWidth > window.innerHeight){
//landscape
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With