What is the purpose of the HTML no-js class?
When Modernizr runs, it removes the "no-js" class and replaces it with "js". This is a way to apply different CSS rules depending on whether or not Javascript support is enabled.
See Modernizer's source code.
Using .no-js class for detection of js
Paul Irish has an excellent blog post on dealing with this problem. Here's the solution from that post:
<!DOCTYPE html>
<html lang='en' class='no-js'>
<head>
<script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js')})(document.documentElement)</script>
...
The key is to make sure you've updated the classnames prior to the browser having a chance to render anything.
Why should I use noscript?
Well, why should you? If you have no use for it, then just don't use it.
The noscript
tag is often used for fallback in advertisments and visitor tracking. If the user has Javascript disabled, a plain image is loaded instead of running the script.
JavaScript - Replacing no-js class with js class not working?
replace
returns the new string, it doesn't modify the original.
Try html.className = 'js'
, since you've already established that it's the only class name with your if
statement.
EDIT: Also, the <html>
tag is already available as document.documentElement
.
document.documentElement.className = 'js';
That's all you need, really. No if
statement, no variable, nothing.
What is the best way to add or remove a class for progressive enhancement?
Example A is invalid HTML.
HTML must be arranged in a strict hierarchy.
An element (e.g. your <div>
) cannot start as a child of an element (e.g. your <noscript>
) without also having the matching end tag inside the same <noscript>
).
With option A eliminated, only option B remains.
Related Topics
How to Make a Radio Button Unchecked by Clicking It
How to Change the Background Image of Div Using JavaScript
The Purpose of Starting an Initial Comment with /*! in JavaScript and CSS Files
Jquery UI 1.10: Dialog and Zindex Option
Show Dynamically Added Navlinks When Added in Bootstrap Navbar
Call Angularjs from Legacy Code
Async Function Not Returning Value, But Console.Log() Does: How to Do
Mismatched Anonymous Define() Module
How to Read 'Native Code' JavaScript Functions
Class VS. Static Method in JavaScript
:Touch CSS Pseudo-Class or Something Similar
How to Limit Options Selected in a HTML Select Box
How to Tell When a CSS Background Image Has Loaded? Is an Event Fired
CSS Float Elements with Unequal Heights Left and Up in Grid
Mutationobserver: New Value in the Format of 'Oldvalue'
Passing Parameters to JavaScript Files
How to Access a JavaScript Variable Using a String That Contains the Name of the Variable