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

289
Visualizações
SVG <object>'s child elements odd behavior

I don't know if this is a bug, undefined behavior or what. But when loading an SVG in with the <object> element, JS and CSS are unpredictable when it comes to accessing child elements of that SVG. Best I show and example:

Here we have our test.svg:

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <rect id="myrect" x="0" y="0" width="100" height="100" />
</svg>

And our test.html:

<object
    style = "max-width:300px"
    type  = "image/svg+xml"
    data  = "test.svg" />

Now when I load the page I can see the black rectangle. When I execute document.querySelector("svg") in the console it comes up null. However when I inspect element on the page and see that there is indeed an SVG in the dom, I execute that command again and this time it returns the svg.

Thus, without any modification the DOM managed to change itself for no reason. What is supposed to happen?

screenshot

Update regarding what browser I was using

I was using Brave Version 1.31.88 Chromium: 95.0.4638.69 (Official Build) (64-bit) while getting the above results. My firefox browser seems to provide a different result, that is the JS never has access to the SVG regardless if I inspect element or not.

Possible workaround (js only)

Using the getSVGDocument() function, I have the ability to modify the style.

document.querySelector("object").getSVGDocument().querySelector("#myrect").style="fill:red";

The above code works so long that you don't inspect element on the rectangle. Works all the time in firefox.

HOWEVER the above solution only works on a fresh download of the object data. If you try to refresh, the above script will not work...

OH MY LORD I DIDN'T REALIZE THE RABBIT HOLE OF CHROME/MOZILLA BUGS I'VE JUST ENTERED

about 4 years ago · Juan Pablo Isaza
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