Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!