9

In order for my webpage to degrade gracefully, I have some CSS that should only be loaded if its corresponding JavaScript will function.

What is the simplest way to load local CSS if and only if the browser has JavaScript enabled?

Also it's quite a big block of CSS, so I'd rather not have to write a JavaScript line to add each attribute. (Oh, and if necessary I can use jQuery).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jon Cox
  • 10,622
  • 22
  • 78
  • 123
  • 2
    I don't mean to be pedantic, but if your CSS is relying on javascript to look correct, it's not really separating presentation from content. – CassOnMars Jul 21 '11 at 16:26
  • Take a look at modernizr.com as well... – binarious Jul 21 '11 at 16:28
  • Maybe he means that it's CSS for elements that will only be present if JavaScript is enabled, like enhanced UI components. – shanethehat Jul 21 '11 at 16:28
  • 1
    @d_r_w I do mean to be pedantic - the CSS doesn't look correct, the HTML does when it is styled by the information stored in the CSS stylesheets. As long as that information is in the stylesheets then it is separated. JS can change the state of the HTML, in which case the new HTML should rely on the CSS to tell it how to look. This is separating the concerns. (All abstractions leak). – edeverett Jul 21 '11 at 16:31
  • I needed the page to be presented differently if javascript didn't work - same content, but different way of view it essentially. (I've expressed that in a way that I'm sure makes perfect sense :P ) – Jon Cox Jul 21 '11 at 16:48

5 Answers5

9

Set a class on the body tag of "noJS". Then use Javascript to remove that class.

For the CSS rules that you want to be used when no JS is present, just use .noJS .myRules {}

edeverett
  • 8,012
  • 33
  • 28
5

In the <head> you can include it with document.write(). I've never tried this, but it should work in theory. No script execution means no stylesheet loaded.

<head>
  <script type='text/javascript>
   document.write("<link rel='stylesheet' href='your.css' media='whatever />");
  </script>
</head>
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
3

You have a few options.

The one I like is to add the following code to the body tag.

<body id="no-js"><script>document.body.id="js"</script>

Then I can target #no-js and #js from my master CSS.

An additional option is to load an extra stylesheet with JavaScript, but that will slow down you initial load, which I try to avoid.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Emil
  • 8,449
  • 3
  • 27
  • 44
2

Check out the body conditionals for html5 boilerplate to see how to employ modernizr.

Also good example here: http://webdesignernotebook.com/css/how-to-use-modernizr/

Then write your css selectors for .no-js .addl-selector {}

Lou Groshek
  • 718
  • 6
  • 23
1

I simply do this:

document.documentElement.className = "js"

This adds class js to <html> tag, then you can target your elements by css using:

.js #someid{}
simPod
  • 11,498
  • 17
  • 86
  • 139