Detect the current Rails environment from JavaScript or CSS

Updated . Posted . Visible to the public. Repeats.

Detecting if a Javascript is running under Selenium WebDriver is super-painful Show archive.org snapshot . It's much easier to detect the current Rails environment instead.

You might be better of checking against the name of the current Rails environment. To do this, store the environment name in a data-environment of your <html>. E.g., in your application layout:

<html data-environment=<%= Rails.env %>>

Now you can say in a piece of Javascript:

if (document.documentElement.dataset.environment == 'test') {
  // Code that should happen in Selenium tests
} else {
  // Code that should happen for other environments
}

Or in your CSS / Sass:

html[data-environment="test"] {
  * { text-transform: none !important; }
}

See also

Henning Koch
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2011-08-04 10:32)