views:

113

answers:

2

I'm building a Drupal theme up and want to know if there is a Drupalish way to add a css file only if the user has js turned off.

This would ideally go in the theme.info file to keep it neat!

Something like this would be ideal:

conditional-stylesheets[if no javascript][all][] = nojs.css

If this isn't possible then I'm going to keep all the css that needs JS out of the css files, and add it dynamically using JS but this seems a bit messy...

Any ideas?

A: 

I don't know drupal that well, but it's a good question either way. According to W3Schools, the <noscript> tag is allowed only within the body element, so that is out.

Have you considered doing it the other way round? i.e. adding a script-specific CSS stylesheet using JavaScript? See starting points for that here.

Pekka
That looks interesting but I think in my particular project using something like jquery css() would be more appropriate - http://api.jquery.com/css/
jsims281
+4  A: 

You don't need conditional comments or noscript-tags for that. By default, Drupal adds a 'js' class to the html element and sets a cookie if javascript is enabled:

// Global Killswitch on the <html> element
if (Drupal.jsEnabled) {
  // Global Killswitch on the <html> element
  $(document.documentElement).addClass('js');
  // 'js enabled' cookie
  document.cookie = 'has_js=1; path=/';
  // Attach all behaviors.
  $(document).ready(function() {
  Drupal.attachBehaviors(this);
  });
 } 

(That's on line 296 in /misc/drupal.js.)

All css selectors that should only apply when js is enabled, can be prefixed with .js. If you want, you can put those css rules in a separate file, but you don't have to.

marcvangend
Thats brilliant. Thanks for the answer and I'm sure it will help others too.
jsims281
You're welcome. Thanks for the feedback.
marcvangend