Skip to content

Instantly share code, notes, and snippets.

@vigneshshanmugam
Last active June 2, 2016 15:15
Show Gist options
  • Save vigneshshanmugam/000632ad4804b769f98d758a509c4802 to your computer and use it in GitHub Desktop.
Save vigneshshanmugam/000632ad4804b769f98d758a509c4802 to your computer and use it in GitHub Desktop.
Check if its Desktop/Mobile without using window.innerWidth
<html>
<style>
@media screen and (max-width: 1024px){
.agent {
opacity: 1 !important;
}
}
.agent {
opacity: 0;
}
</style>
<script>
var isDesktop = true;
var agent = document.querySelector('.agent');
var agentValue = window.getComputedStyle(agent);
var result = document.querySelector('.result');
function checkDesktop() {
if (agentValue.opacity === '1') {
isDesktop = false;
} else {
isDesktop = true;
}
result.innerHTML = 'Desktop - ' + isDesktop;
console.log('Desktop', isDesktop);
}
window.addEventListener('resize', () => {
checkDesktop();
});
checkDesktop();
</script>
<body>
<div class="agent"></div>
<h3 class="result"></h3>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment