动态加载css文件使用javascript与回调没有jQuery

前端之家收集整理的这篇文章主要介绍了动态加载css文件使用javascript与回调没有jQuery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用javascript动态加载一个css文件,不能使用任何其他的js库(例如jQuery)。

css文件加载,但我似乎无法得到一个回调工作。以下是我正在使用的代码

  1. var callbackFunc = function(){
  2. console.log('file loaded');
  3. };
  4. var head = document.getElementsByTagName( "head" )[0];
  5. var fileref=document.createElement("link");
  6. fileref.setAttribute("rel","stylesheet");
  7. fileref.setAttribute("type","text/css");
  8. fileref.setAttribute("href",url);
  9.  
  10. fileref.onload = callbackFunc;
  11. head.insertBefore( fileref,head.firstChild );

使用以下代码添加脚本标签来加载js文件可以起作用并触发回调:

  1. var callbackFunc = function(){
  2. console.log('file loaded');
  3. };
  4.  
  5. var script = document.createElement("script");
  6.  
  7. script.setAttribute("src",url);
  8. script.setAttribute("type","text/javascript");
  9.  
  10. script.onload = callbackFunc ;
  11.  
  12. head.insertBefore( script,head.firstChild );

我在这里做错了吗?可以帮助我实现这一点的任何其他方法将不胜感激?

解决方法

不幸的是,大多数现代浏览器中的样式表都没有负载支持。有一个解决方案,我发现一些谷歌。

引用自:http://thudjs.tumblr.com/post/637855087/stylesheet-onload-or-lack-thereof

基础

最基本的实现可以在一个简单的30行框架中独立完成 – JavaScript代码

  1. function loadStyleSheet( path,fn,scope ) {
  2. var head = document.getElementsByTagName( 'head' )[0],// reference to document.head for appending/ removing link nodes
  3. link = document.createElement( 'link' ); // create the link node
  4. link.setAttribute( 'href',path );
  5. link.setAttribute( 'rel','stylesheet' );
  6. link.setAttribute( 'type','text/css' );
  7.  
  8. var sheet,cssRules;
  9. // get the correct properties to check for depending on the browser
  10. if ( 'sheet' in link ) {
  11. sheet = 'sheet'; cssRules = 'cssRules';
  12. }
  13. else {
  14. sheet = 'styleSheet'; cssRules = 'rules';
  15. }
  16.  
  17. var interval_id = setInterval( function() { // start checking whether the style sheet has successfully loaded
  18. try {
  19. if ( link[sheet] && link[sheet][cssRules].length ) { // SUCCESS! our style sheet has loaded
  20. clearInterval( interval_id ); // clear the counters
  21. clearTimeout( timeout_id );
  22. fn.call( scope || window,true,link ); // fire the callback with success == true
  23. }
  24. } catch( e ) {} finally {}
  25. },10 ),// how often to check if the stylesheet is loaded
  26. timeout_id = setTimeout( function() { // start counting down till fail
  27. clearInterval( interval_id ); // clear the counters
  28. clearTimeout( timeout_id );
  29. head.removeChild( link ); // since the style sheet didn't load,remove the link node from the DOM
  30. fn.call( scope || window,false,link ); // fire the callback with success == false
  31. },15000 ); // how long to wait before failing
  32.  
  33. head.appendChild( link ); // insert the link node into the DOM and start loading the style sheet
  34.  
  35. return link; // return the link node;
  36. }

这将允许您使用onload回调函数加载样式表,如下所示:

  1. loadStyleSheet( '/path/to/my/stylesheet.css',function( success,link ) {
  2. if ( success ) {
  3. // code to execute if the style sheet was loaded successfully
  4. }
  5. else {
  6. // code to execute if the style sheet Failed to successfully
  7. }
  8. } );

或者如果你想要回调来维护它的范围/上下文,你可以做一些这样的事情:

  1. loadStyleSheet( '/path/to/my/stylesheet.css',this.onComplete,this );

猜你在找的CSS相关文章