Solution in Less.js v1.0.31

To address this limitation, version 1.0.31 of Less.js introduced a method called less.refreshStyles(). This method enables the recompilation of

In JavaScript:

less.refreshStyles();
","image":"http://www.luping.net/uploads/20241116/17317674096738ac716c563.jpg","datePublished":"2024-11-16T23:10:36+08:00","dateModified":"2024-11-16T23:10:36+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How can I dynamically load Less.js rules after the initial page load?

How can I dynamically load Less.js rules after the initial page load?

Published on 2024-11-16
Browse:322

How can I dynamically load Less.js rules after the initial page load?

Loading Less.js Rules Dynamically

Incorporating Less.js into a website can enhance its styling capabilities. However, one limitation encountered is the requirement to load all LESS stylesheets before the Less.js script. This can pose a challenge when certain styles need to be loaded dynamically post initial page load.

Current Limitations

Presently, Less.js dictates that the order of loading external stylesheets and the script play a crucial role. Reversing the order, as shown below, fails to load the style.less file in both Firefox and Chrome:

Solution in Less.js v1.0.31

To address this limitation, version 1.0.31 of Less.js introduced a method called less.refreshStyles(). This method enables the recompilation of

Implementing Dynamic Loading

To implement dynamic loading of Less.js rules, follow these steps:

  1. Ensure you are using Less.js version 1.0.31 or later.
  2. Include the
  3. Add the LESS rules you want to apply dynamically within the
  4. Call less.refreshStyles() to recompile and apply the new rules.

Example:


  

In JavaScript:

less.refreshStyles();
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3