Created
June 2, 2020 13:31
-
-
Save JPustkuchen/19b555500e8fe147188b2330c8ba37de to your computer and use it in GitHub Desktop.
JavaScript X-Browser reload on orientation change using MatchMedia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RELOAD ON ORIENTAtiON CHANGE: | |
var mqp = window.matchMedia("(orientation: portrait)"); | |
// Detect initial orientation | |
var is_portrait = mqp.matches; | |
var isFullscreen = document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement; | |
// Add a media query change listener | |
mqp.addListener(function(m) { | |
// Check if orientation changed: | |
if(m.matches) { | |
// Changed to portrait | |
if (!is_portrait && !isFullscreen) { | |
// Was landscape before and is not fullscreen | |
// => Reload | |
window.location.reload(); | |
} | |
is_portrait = true; | |
} else { | |
// Changed to landscape! | |
if (is_portrait && !isFullscreen) { | |
// Was portrait before and is not fullscreen | |
// => Reload | |
window.location.reload(); | |
} | |
is_portrait = false; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment