Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to reference a css stylesheet within javascript?

    Does anyone know of any websites/tutorials/resources/information on how to reference a css stylesheet within javascript code, or actually add all the css into the js?
    Reason being is because I'm using a content management application at work that automatically 'validates' (removes) any css. So I'm trying to add it into the javascript as a workaround.

    Any other suggestions?

    Thanks

    //Edit: Wouldn't you know it, as soon as i posted this thread I tried a different google search and I found the code below which seems to work just fine, however I'm not a javascript guru so even though it works fine, is there anything that should be changed below to optimize it? If not, i'll just use it as is.

    Code:
    var headID = document.getElementsByTagName("head")[0];         
    var cssNode = document.createElement('link');
    cssNode.type = 'text/css';
    cssNode.rel = 'stylesheet';
    cssNode.href = 'path_to_css.css';
    cssNode.media = 'screen';
    headID.appendChild(cssNode);
    Last edited by lazywolfy; 06-24-2009 at 10:30 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •