开发者

Drupal - special css file if JS is turned off?

开发者 https://www.devze.com 2022-12-31 04:53 出处:网络
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.

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 b开发者_StackOverflow中文版it messy...

Any ideas?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号