SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Link to CSS file from JS file

    Can someone tell me how to do it so that only browsers with javascript get the css file. I want to put it in a js file as it will make managing it easier. So in other words, don't provide alternatives

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can lazy load a CSS file with javascript.
    For example:
    Code:
    window.onload = function() {
       var newCSS = document.createElement('link');
       newCSS.href = 'css/for-javascript-users.css'; //path to CSS file
       newCSS.type = 'text/css';
       newCSS.rel = 'stylesheet';
    
        //Add CSS to Document
       var head = document.getElementsByTagName('head')[0];
       if(!head) return;
       head.appendChild(newCSS);
     }

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are not using an XHTML doctype then you can do this:
    Code:
    document.write("<style type='text/css'>.myStyleOverrides {...}<\/style");
    I've used this alot and I see nothing wrong with using this method for HTML doctypes - it is as cross-browser as you can get. But you can't use this method in XHTML... but then, most of the people who use an XHTML doctype are not really serving XHTML


Bookmarks

Posting Permissions

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