如何在用户将手机切换为横向时刷新网页



我正在为android和iPhone做一个web应用程序。我的问题是,当用户转到横向时,我想刷新网页。由于我是javascript的初学者,我不知道如何做到这一点。请帮我

您可以使用orientationchange jQuery Mobile事件,然后执行

window.location.reload(); 

以刷新页面。

例如:

$(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>");
}
});

您可以使用计时器并定期检查:

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

最新更新