Javascript doOnOrientationChange: ne peut pas réparer un bug chargé de la page

J'ai utilisé un .js pour éviter la vue paysage à partir d'un appareil mobile. J'ai édité une image blanche en plein écran en disant que "ce site ne semble pas être visualisé en mode paysage, passez votre appareil" chaque fois que je tourne mon appareil de portrait en paysage. Il fonctionne sauf lorsque je charge une page et je suis déjà en mode paysage. Une idée sur la façon de le réparer? Merci

 <script> (function() { 'use strict'; var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return navigator.userAgent.match(/Opera Mini/i); }, Windows: function() { return navigator.userAgent.match(/IEMobile/i); }, any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); } }; if (isMobile.any()) { doOnOrientationChange(); window.addEventListener('resize', doOnOrientationChange, 'false'); } function doOnOrientationChange() { var a = document.getElementById('alert'); var b = document.body; var w = b.offsetWidth; var h = b.offsetHeight; (w / h > 1) ? (a.className = 'show', b.className = 'full-body') : (a.className = 'hide', b.className = ''); } })(); </script> 

Mettre à jour :

J'ai essayé d'ajouter window.orientation au script mais quelque chose ne va pas

 if (orientation === "landscape-primary") { doOnOrientationChange(); window.addEventListener('resize',doOnOrientationChange,'false'); } window.onload(doOnOrientationChange()); 

Vous devez déplacer la fonction doOnOrientationChange() dehors de l'autre, puis appelez-la sur la page. Comme cela, cela devrait fonctionner:

 <script> function checkMobile() { var isMobile = false; if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone|iPad|iPod/i) || navigator.userAgent.match(/Opera Mini/i) || navigator.userAgent.match(/IEMobile/i)) { isMobile = true; } return isMobile; } function doOnOrientationChange() { var a = document.getElementById('alert'); var b = document.body; var w = b.offsetWidth; var h = b.offsetHeight; if (checkMobile()) { (w / h > 1) ? (a.className = 'show', b.className = 'full-body') : (a.className = 'hide', b.className = ''); } else { a.className = 'hide'; b.className = ''; } } window.onload = doOnOrientationChange(); window.addEventListener('resize', doOnOrientationChange, 'false'); </script>