简体   繁体   English

我可以使用“load.js”库加载CSS吗?

[英]Can I Load CSS Using the “load.js” Library?

I recently found out about load.js , but I can't seem to find any indication of whether or not this is possible... (Note: I can't find a 'load.js' tag..) 我最近发现了load.js ,但我似乎无法找到是否可能的任何迹象...(注意:我找不到'load.js'标签..)

I've got load.js successfully loading all my JS files, so I know it works. 我已经成功加载了所有JS文件的load.js,所以我知道它有效。 Has anyone got it working for loading CSS files as well? 有没有人得到它加载CSS文件?

Update: remyabel's solution worked perfectly for loading the physical files, but it seems there are a few quirks to this process... 更新: remyabel的解决方案非常适合加载物理文件,但似乎这个过程有一些怪癖......

For some reason, the order in which the CSS files are loaded and whether they're all done in one load(file1,file2); 出于某种原因,加载CSS文件的顺序以及它们是否都在一次load(file1,file2); or in stages with load(file1).then(file2); 或者在load(file1).then(file2);阶段load(file1).then(file2); seems to affect how the style rules are applied to the markup. 似乎影响样式规则应用于标记的方式。 I'm going to set up a few test cases on my local machine to try work out how or why this happens, but for now at least the files are being loaded. 我将在我的本地计算机上设置一些测试用例,以尝试解决这种情况发生的原因或原因,但至少现在正在加载文件。

Final Note: 最后的说明:

Following on from the solution posted below, I've decided to use head.appendChild(script); 继下面发布的解决方案之后,我决定使用head.appendChild(script); instead of head.insertBefore(script, head.firstChild); 而不是head.insertBefore(script, head.firstChild); to add the CSS elements to the DOM (still uses the original method for JS files). 将CSS元素添加到DOM(仍然使用JS文件的原始方法)。

This doesn't affect the order in which files are fetched and processed, but it makes Load.js insert my CSS links in the same order they were listed and at the end of the header instead of the beginning. 这不会影响提取和处理文件的顺序,但它会使Load.js按照列出的顺序和标题末尾而不是开头插入我的CSS链接。

Direct from the source code 直接来自源代码

   function asyncLoadScript(src) {
        return function (onload, onerror) {
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = src;

My suggestion is to modify the script (which doesn't seem to contain much) to mirror the function but for a link tag, rather than a script tag. 我的建议是修改脚本(似乎不包含太多)来镜像函数,但是对于链接标记,而不是脚本标记。

to reflect OP's comment 反映OP的评论

The script is built on top of chain.js so it may be more complicated than expected. 该脚本构建在chain.js之上,因此它可能比预期的更复杂。

Unless you want something else, I'm pretty sure what I wrote above is what you need to change, so it would look like: 除非你想要别的东西,我很确定我上面写的是你需要改变的东西,所以它看起来像:

   function asyncLoadScript(src) {
        return function (onload, onerror) {

// Get file extension
var ext = src.split('.').pop();
if (ext == "js")
{
                var script = document.createElement('script');
                script.type = 'text/javascript';
                script.src = src;
} else if (ext == "css")
{
                var script = document.createElement('link');
                script.type = 'text/css';
                script.href = src;
                script.rel = "stylesheet";
}

Theoretically that should work. 从理论上讲应该有效。 Make another comment if it doesn't work. 如果不起作用,请另外发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM