less.js lazy sheet load

前端 未结 6 1164
天涯浪人
天涯浪人 2021-02-06 00:07

I\'m wondering if there is a way to load a single less sheet sometime after a page load. This question has an answer that explains how to reload all the sheets, but for my use-

6条回答
  •  猫巷女王i
    2021-02-06 00:40

    All you have to do is add a new script tag to the DOM. It will then be downloaded and executed.

    function addScript (id, url)    //  Adds scripts to the DOM
    {
        var s = document.createElement('script');
        s.id = id;
        if (url)    s.src=url;
        s.type='text/javascript';
    
        document.getElementsByTagName('head')[0].appendChild(s)
        return s;
    }
    

    This what I use on my site but I call it on parse. For you to use it as a lazy loader it should work fine for you to call it onload or a similar way.

提交回复
热议问题