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

227
Visualizações
Iframe not loading the css from link tag under same domain in chrome browser

I'm dynamically adding a iframe to html document using javascript.

var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentWindow.document.body.innerHTML = document.querySelector('#section-to-print').innerHTML;

When I add css using link tag from same domain to iframe head, the styles are not loading.

let link = document.createElement("link");
link.href = "https://localhost:5001/css/invoice-receipt.css";      /**** your CSS file ****/
link.rel = "stylesheet";
link.type = "text/css";
iframe.contentWindow.document.head.appendChild(link);

But when I add the css using style tag it works.

var styles = document.createElement("style");
styles.innerHTML = `html, body {
                width: 2.7in;
                margin: 0 auto;
                height: max-content;
            }`;
iframe.contentWindow.document.head.appendChild(styles);

The problem is that I want to load different stylesheet dynamically but loading via link is not working. Please assist on what I'm missing.

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

0

I made this to work using the below script.

var bodyScript = document.createElement("script");
    bodyScript.innerHTML = `fetch('https://localhost:5001/css/invoice-receipt.css')
                            .then(response => response.text())
                            .then(data => {
                                var head = document.head || document.getElementsByTagName('head')[0],
                                    style = document.createElement('style');
                                head.appendChild(style);
                                style.type = 'text/css';
                                style.appendChild(document.createTextNode(data));
                            });`;
    iframe.contentWindow.document.body.appendChild(bodyScript);
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