10

I want to define .exampleclass img {height:250px} if javascript is not enabled. Is their anyway to undo this in javascript / jquery?

ThomasReggi
  • 42,912
  • 63
  • 199
  • 343

8 Answers8

23

You could probably use HTML's noscript tag.

<noscript>
    <style type="text/css">
        .exampleclass img {height:250px}
    </style>
</noscript>

EDIT: I'm going to actually side with Stephen's answer as being the best. While the above might work, it might not be valid/following best practices.

Stephen answered:

put a "no-js" class on your body element, and then remove it with JS at load, and use .no-js in the selector for your CSS

The class could also be placed on your html element and then all styling that should appear when JavaScript is not available can be prefixed with html.no-js before what the selector would be otherwise. This is what HTML5 Boiler Plate does, for example.

Kyle
  • 2,727
  • 2
  • 17
  • 23
  • I believe you can place it within the head tags. It would probably be best to define the style in the head. Edit: If it is overriding the class definition, make sure that this is after the other class is defined. – Kyle Jan 12 '11 at 06:00
  • While this works, I actually recommend alex's solution over this one. – Kyle Aug 09 '11 at 03:32
13

put a "no-js" class on your body element, and then remove it with JS at load, and use .no-js in the selector for your CSS

Stephen
  • 17,156
  • 4
  • 29
  • 31
7

Try

<noscript>
    <style type="text/css">
        .exampleclass img {height:250px}
    </style>
</noscript>

Noscript

rahul
  • 174,563
  • 47
  • 223
  • 254
5

Best not to mess with CSS via JS in that way. Try this:

<noscript>

    <link href="no-js.css" type="text/css" rel="stylesheet" />

</noscript>
JTG
  • 7,817
  • 6
  • 26
  • 37
Fordi
  • 2,544
  • 22
  • 19
3

I use something like this...

JavaScript

document.body.className += ' javascript';

jQuery

$('body').addClass('javascript');

CSS

.exampleclass img {height: 250px}

.javascript .exampleclass img {height: 500px}
alex
  • 438,662
  • 188
  • 837
  • 957
  • 1
    [According to MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript), `style` within `noscript` within `head` is valid. – Josh Kelley Sep 05 '14 at 21:11
  • @JoshKelley Good to know, I don't know where I got that piece of information from. I'll remove it. – alex Sep 07 '14 at 23:58
3

One day we may have this

Not currently active in Chrome 83 Win10

https://developer.mozilla.org/en-US/docs/Web/CSS/@media/scripting

p {
  color: lightgray;
}

@media (scripting: none) {
  .script-none {
    color: red;
  }
}

@media (scripting: initial-only) {
  .script-initial-only {
    color: red;
  }
}

@media (scripting: enabled) {
  .script-enabled {
    color: red;
  }
}
<p class="script-none">You do not have scripting available. :-(</p>
<p class="script-initial-only">Your scripting is only enabled during the initial page load. Weird.</p>
<p class="script-enabled">You have scripting enabled! :-)</p>
mplungjan
  • 134,906
  • 25
  • 152
  • 209
1

The most elegant way is to have in your CSS something like:

.exampleclass img {height:250px}
.js .exampleclass img {height:auto;}

and in your JS:

var dd = document.documentElement;
dd.className += ((dd.className == "") ? "js" : " js");

so that the second CSS rule will override the first one in case JS is enabled

sebarmeli
  • 17,141
  • 6
  • 31
  • 39
1

In the future (if and when browsers start supporting it), it will be possible to use the scripting CSS media feature:

@media (scripting: none) {
  // Styles to apply if JS is disabled
}
D. Pardal
  • 4,876
  • 1
  • 13
  • 32