使用带有回调的 javascript 动态加载 css 文件,无需 jQuery
我正在尝试使用 javascript 动态加载 css 文件,并且不能使用任何其他 js 库(例如 jQuery)。
css 文件已加载,但我似乎无法获得回调来为其工作。下面是我正在使用的代码
var callbackFunc = function(){
console.log('file loaded');
};
var head = document.getElementsByTagName( "head" )[0];
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", url);
fileref.onload = callbackFunc;
head.insertBefore( fileref, head.firstChild );
使用以下代码添加脚本标记来加载 js 文件可以工作并触发回调:
var callbackFunc = function(){
console.log('file loaded');
};
var script = document.createElement("script");
script.setAttribute("src",url);
script.setAttribute("type","text/javascript");
script.onload = callbackFunc ;
head.insertBefore( script, head.firstChild );
我在这里做错了什么吗?任何其他可以帮助我实现这一目标的方法将不胜感激?
I am trying to load a css file dynamically using javascript and cannot use any other js library (eg jQuery).
The css file loads but I can't seem to get a callback to work for it. Below is the code I am using
var callbackFunc = function(){
console.log('file loaded');
};
var head = document.getElementsByTagName( "head" )[0];
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", url);
fileref.onload = callbackFunc;
head.insertBefore( fileref, head.firstChild );
Using the following code to add a script tag to load a js file works and fires a callback:
var callbackFunc = function(){
console.log('file loaded');
};
var script = document.createElement("script");
script.setAttribute("src",url);
script.setAttribute("type","text/javascript");
script.onload = callbackFunc ;
head.insertBefore( script, head.firstChild );
Am I doing something wrong here? Any other method that can help me achieve this would be much appreciated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不幸的是,大多数现代浏览器都不支持样式表加载。我通过谷歌搜索找到了一个解决方案。
引用自: http://thudjs .tumblr.com/post/637855087/stylesheet-onload-or-lack-thereof
基础知识
最基本的实现可以通过区区 30 行独立于框架的 JavaScript 代码完成:
这将允许您使用 onload 回调函数加载样式表,如下所示:
或者,如果您希望回调保持其范围/上下文,您可以执行如下操作:
Unfortunately there is no onload support for stylesheets in most modern browsers. There is a solution I found with a little Googling.
Cited from: http://thudjs.tumblr.com/post/637855087/stylesheet-onload-or-lack-thereof
The basics
The most basic implementation of this can be done in a measely 30 lines of — framework independent — JavaScript code:
This would allow you to load a style sheet with an onload callback function like this:
Or if you want to your callback to maintain its scope/ context, you could do something kind of like this:
这种普通的 JS 方法适用于所有现代浏览器:
This vanilla JS approach works in all modern browsers:
前段时间我为此创建了一个库,它的名字是 Dysel,我希望它有帮助
示例:
https://jsfiddle.net/sunrising/qk0ybtnb/
Some time ago i made a library for this, it's called Dysel, i hope it helps
Example:
https://jsfiddle.net/sunrising/qk0ybtnb/
您可以在 html 文件中创建一个空的 css 链接,并为该链接指定一个 ID。例如
,然后使用 ID 名称调用它并更改“href”属性
You can make an empty css link in your html file and give the link an ID. e.g
then call it with ID name and change the 'href' attribute
yepnope.js 可以加载 CSS 并在完成时运行回调。例如
yepnope.js can load CSS and run a callback on completion. e.g.
我们是这样做的。通过使用“requestAnimationFrame”(如果无效,则回退到简单的“load”事件)。
顺便说一句,这是谷歌在其“页面速度”手册中推荐的方式:
https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery
Here's how we do it. By using "requestAnimationFrame" (or fallback to simple "load" event if its not avail).
By the way, this is the way Google recommends in their "page-speed" manual:
https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery
老问题的新答案:
您可以简单地使用 AJAX 请求 CSS 文件的文本并将其放入
标记中。当样式被附加到 DOM 后,它们立即可用。
这是我想出的一个脚本:
New answer to an old question:
You can simply request the text of the CSS file with AJAX and put it in a
<style>
tag. When the styles have been appended to the DOM they are available immediately.Here's a script I came up with: