0

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?

jsims281
  • 2,206
  • 2
  • 30
  • 57

2 Answers2

4

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
  • 5,592
  • 4
  • 22
  • 32
0

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.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 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 May 24 '10 at 11:42