Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
Detect both mobile device and orientation to add a code

I want to show a text to the users if they are on mobile device and the orientation is portrait.

For detecting mobile device, I am using this script:

if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
  document.write("mobile");
}else{
  document.write("not mobile");
}

and for portrait mode, I am using this CSS:

@media (orientation: portrait) {
    .land-msg {display: block;}
}

But I can not combine both to work with each other.

Is thee a way I can make it to check both mobile device and orientation in a single code?

Media screen size won't work as I don't want it to show on desktop browser resize.

Any help appreciated.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I have used the below code to make it work:

if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
    if(window.innerHeight > window.innerWidth){
        jQuery(".land-msg").css("display", "block");
    }
}else{}

Thank you.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use matchMedia to detect orientation (min/max-width etc)

const isMobile = (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent))


function matchMediaHandler(query) {
  if (query.matches) { // If media query matches (portrait)
    document.body.style.backgroundColor = "orange"
    document.body.innerText = isMobile ? 'Mobile Portrait' : 'Desktop Portrait' 
  } else {
    document.body.style.backgroundColor = "yellowgreen"
    document.body.innerText = isMobile ? 'Mobile Landscape' : 'Desktop Landscape'    
  }
}


// orientation query   
const orientation = matchMedia("(orientation: portrait)")

// add listener 
orientation.addListener(matchMediaHandler)

// trigger on load 
matchMediaHandler(orientation)

about 4 years ago · Juan Pablo Isaza Relatório

0

There are pure CSS ways to detect mobile devices in a way which is better than userAvent sniffing. To word it another way: never ever do userAgent sniffing. I promise you're doing it wrong.

/* smartphones, touchscreens */
@media (hover: none) and (pointer: coarse) and (orientation: portrait) {
    /* ... */
}

Portions of this was taken from here: https://ferie.medium.com/detect-a-touch-device-with-only-css-9f8e30fa1134

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda