Load Css Files Dynamically Via Javascript


/ Published in: JavaScript
Save to your folder(s)

changed to detect sring sheet first, then to try object extension.


Copy this code and paste it in your HTML
  1. var CSSLoader = {
  2. addStylesheets: function(sheets, elm){
  3. var l = sheets.length;
  4. if( !l ) return;
  5. elm = $(elm) || $$('head')[0];
  6. if( !elm ) return;
  7.  
  8. for( var i=0; i<l; i++){
  9. var sheet = sheets[i];
  10. var info = {
  11. rel: 'stylesheet',
  12. type: 'text/css',
  13. media: 'screen'
  14. }
  15. if( Object.isString(sheet)){
  16. Object.extend(info, {href: sheet});
  17. }else{
  18. try{
  19. Object.extend(info, sheet);
  20. }catch(e){
  21. continue;
  22. }
  23. }
  24. var temp = new Element('link', info);
  25. elm.insert(temp);
  26. }
  27. }
  28. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.