Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created August 17, 2018 19:30
Show Gist options
  • Save prof3ssorSt3v3/2b3f9b7d04362558a5ea8a95d96a930a to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/2b3f9b7d04362558a5ea8a95d96a930a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Screen Orientation</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="main.css">
</head>
<body>
<header>
<h1>Screen Orientation</h1>
</header>
<main>
<div id="output"></div>
</main>
<script>
let orn = getOrientation();
let out = document.getElementById('output');
out.textContent = orn;
function getOrientation() {
let _orn = screen.msOrientation ||
(screen.orientation || screen.mozOrientation).type;
switch(_orn){
case 'portrait-primary':
case 'portrait-secondary':
break;
case 'landscape-primary':
console.log('This is the laptop/desktop version')
break;
case 'landscape-secondary':
break;
case undefined:
//not supported
break;
default:
//something unknown
}
return _orn;
}
window.addEventListener('orientationchange', (ev)=>{
orn = getOrientation();
out.textContent = orn;
console.dir(ev)
})
</script>
</body>
</html>
@nxone
Copy link

nxone commented Dec 23, 2020

Stupid question but I'm trying to use this in a ReactJS app and it seems to trigger an error:

index.js:78 Uncaught TypeError: Cannot read property 'screen' of undefined
at getOrientation (index.js:78)
at Module. (index.js:73)
at Module../src/pages/index.js (index.js:100)
at webpack_require (bootstrap:789)
at fn (bootstrap:100)
at Object. (sync-requires.js:9)
at Object../.cache/this_is_virtual_fs_path/$virtual/sync-requires.js (sync-requires.js:4)
at webpack_require (bootstrap:789)
at fn (bootstrap:100)
at Module. (api-runner-browser.js:7)
at Module../.cache/app.js (app.js:17)
at webpack_require (bootstrap:789)
at fn (bootstrap:100)
at Object.0 (reset.scss?d32a:45)
at webpack_require (bootstrap:789)
at bootstrap:856
at bootstrap:856

Do you know what happens to be the issue here?

Screenshot 2020-12-23 at 15 15 54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment