Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPustkuchen/19b555500e8fe147188b2330c8ba37de to your computer and use it in GitHub Desktop.
Save JPustkuchen/19b555500e8fe147188b2330c8ba37de to your computer and use it in GitHub Desktop.
JavaScript X-Browser reload on orientation change using MatchMedia
// 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