简体   繁体   中英

How to do Refresh a web page when user turns mobile to landscape

I am doing a web application for android and iPhone. My problem is, i want to do refresh the webpage, when user goes to landscape. Since i'm a beginner in javascript i don't know how to do this. please help me

You could use the orientationchange jQuery Mobile event and then do a

window.location.reload(); 

to refresh the page.

For example:

$(function(){

// Set Inital orientation
// get the initial orientation from window which
// returns 0 for portrait and 1 for landscape
if(window.orientation == 0){
    var ori = "portrait";
}else{
    var ori = "landscape";
}
changeOrientation(ori);

// Orientation Change
// When orientation changes event is triggered
// exposing an orientation property of either
// landscape or portrait
$('body').bind('orientationchange',function(event){
    changeOrientation(event.orientation)
})

// Change the style dependengt on orientation
function changeOrientation(ori){
    // Remove all classes separated by spaces
    $("#orientation").removeClass('portrait landscape');
    $("#orientation").addClass(ori);
    $("#orientation").html("<p>"+ori.toUpperCase()+"</p>");
}

});

You could use a timer and periodically check:

if (window.innerHeight > window.innerWidth) {
    // Redirect code, window.location...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM