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

167
Visualizações
HTML object is blocking eventlistener

I have imported a svg as an object in HTML:

  <object data="mySVG.svg" type="image/svg+xml" id="circle">
    <img src="mySVG.svg" />
  </object>

and I am trying to set an eventlistener on the whole page:

window.addEventListener('click', function(){
   alert('Hello')
})

The problem is that the object blocks the eventlistener and when the user clicks on the image the alert is not fired. But when the user clicks anywhere else or over other elements, the alert is fired. How can I make it so the object is acting as the other elements and doesn't block the eventlistener?

I tried wait after the object is beaing loaded and then set the eventlistener but it didn't work.

If I import the SVG directly into HTML with svg tag it works, but the svg is quit big and it makes the HTML code really messy. I can't use the img tag either becuase I am also interacting with parts of the SVG with JS later.

As it can be seen in this codepen I've made: https://codepen.io/Dimertuper/pen/rNJoLrK (When you click outside the image it triggers, inside the image it doesn't)

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

0

Per OP's requirements -

  • Needs to be able to click on window/document and receive the alert message even when clicking on the HTML object tag.

We can do this by removing the object tag as a clickable element with CSS pointer-events: none;.

object {
   pointer-events: none;
}

https://codepen.io/LTFoReal/pen/NWyerZg?editors=1111

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this:

document.addEventListener('click', function(){
   alert('Hello')
})

There is a difference between document and window. document is used to access any HTML element on the web page, whereas window represents the frame of the browser. So when you click on the button (HTML element) by using window, it doesn't work.

See also: Differences between document and window objects

about 4 years ago · Juan Pablo Isaza Relatório

0

This link has work around. Using a transparent div to cover object image, or directly use svg image instead.

I checked the specification of object element. It's for embeded external content usage. So it has ability to load a full document, your case is load as image. The available property to do event binding for this element is contentDocument or getSvgDocument(). Both are null under your case, as it's loaded as svg image.

document.getElementsByTagName("object")[0].contentDocument

Check this link for detail. Hope this helps you.

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