Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda