By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

Change/Add Style Sheet with JavaScript

P: 2
Hello all,

I have a variable 'css' that reads a url for a style sheet. I need to add the 'css' style sheet to the cascade using JavaScript.

Is there a clean way to do that?


Jan 7 '08 #1
Share this Question
Share on Google+
2 Replies

P: 428
Expand|Select|Wrap|Line Numbers
  1. function addLink(url){
  2.     var el= document.createElement('link');
  3.     var atts= {rel:'stylesheet',type:'text/css',media:'screen',href:url};
  4.     for(var p in atts) el[p]= atts[p];
  5.     document.getElementsByTagName('head')[0].appendChild(el);
  6.     return el;
  7. }
You can make this a method of some object instead of a global function, if that seems cleaner to you.
Jan 7 '08 #2

P: 2
Thanks, I'll give it a try.
Jan 7 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.