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

175
Visualizações
How to check class properties using javascript?

I am developing a ads blocker script and I have placed a div with classes like this =

<div id="detect" class="ad ads adsbox ad-placement doubleclick ad-placeholder ad-badge"></div>

If ads blocker is present on the browser then it will make the css attributes display none by the browser.

So here I want to check if this class is displayed or not on the browser. If the class is displayed then I want to show text that says ads blocker not available and if the div element is hidden with none properties with ads blocker extension does then we will display ads blocker is present text.

How can I detect the property of display: none !important; this attribute with javascript?

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

0

It sounds like you're looking for the Window.getComputedStyle() method which:

[...] returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain.

In this case, you're looking for the value of the display property. You may have to wait for the document's load event depending on how your adblocker works.

If you need to check that the !important flag is used, you can additionally call the CSSStyleDeclaration.getPropertyPriority() method. The following sample shows both:

const canary = document.querySelector('div#detect');
const style = getComputedStyle(canary);

if (style.display === 'none') {
  console.log('Adblocker active.');
  
  const isImportant = style.getPropertyPriority('display') === 'important';
  console.log(`Important: ${isImportant}`);
}
<div id="detect" class="ad ads adsbox ad-placement doubleclick ad-placeholder ad-badge"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try something like this:

window.addEventListener('DOMContentLoaded', (event) => {
  let ads = document.querySelectorAll('.ad, .ads, .adsbox, .ad-placement, .doubleclick, .ad-placeholder, .ad-badge');

  for(let i in ads){
    if(ads[i].style.display === 'none') document.getElementById('presence').innerHTML = 'Adblock Present'
  }
});
<div id="detect" class="ad ads adsbox ad-placement doubleclick ad-placeholder ad-badge">Hello</div>


<div id="detect" style="display: none !important;" class="ad ads adsbox ad-placement doubleclick ad-placeholder ad-badge">This is a hidden ad</div>


<b id='presence'></b>

However, i recommend you to also check this answer so you can refine the method of getting styles from elements. ie getComputedStyle

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