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

117
Visualizações
Run a function after CSS link's media attribute is changed

I'm trying to dynamically change the media attribute of a CSS link tag and I need to run a function after the new CSS is applied.

I have a CSS stylesheet link tag

<link href="print.css" rel="stylesheet" media="print">

Inside my JavaScript code, I want to change the media attribute from print to all, and after the new style changes are applied to my page, I need to run a function.

Right now, I'm doing the following:

if (myCSSlink.getAttribute("media") === "print") {
                myCSSlink.setAttribute("media", "all");
                myCSSlink.onload = () => {
                    //do some work
                };

}

At this point, I'm assuming that after the media attribute is changed, the CSS loads again. But my code inside the anonymous arrow function doesn't seem to work. How can I approach this?

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

0

You should assign an onload handler to do both - change the media attribute and run your custom onload function.

function onLoad(link) {
    link.setAttribute('media', 'all');
    yourCustomFunction();
}
<link href="print.css" rel="stylesheet" media="print" onload="onLoad(this);">

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