oio11: (Default)
[personal profile] oio11
javascript - What is the purpose of the HTML "no-js" class?


I notice that in a lot of template engines, in the HTML5 Boilerplate, in various frameworks and in plain php sites there is the no-js class added onto the <HTML> tag.
Why is this done? Is there some sort of default browser behavior that reacts to this class? Why include it always? Does that not render the class itself obsolete, if there is no no-"no-js" case and html can be addressed directly?
Here is an example from the HTML5 Boilerplate index.html:
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]--> <!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]--> <!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]--> <!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]--> <!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
As you can see, the <html> element will always have this class. Can someone explain why this is done so often?
asked Jul 17 '11 at 14:35
Swader
3,57352870

5 Answers


When Modernizr runs, it removes the "no-js" class and replaces it with "js". This is a way to know in your CSS whether or not Javascript support is enabled.
answered Jul 17 '11 at 14:37
...  The no-js class is used by the Modernizr feature detection library. When Modernizr loads, it replaces no-js with js. If JavaScript is disabled, the class remains. This allows you to write CSS which easily targets either condition.
Remove "no-js" class from element, if it exists: docElement.className=docElement.className.replace(/\bno-js\b/,'') + ' js';
Here is a blog post by Paul Irish describing this approach:http://www.paulirish.com/2009/avoiding-the-fouc-v3/

I like to do this same thing, but without Modernizr. I put the following <script> in the <head>to change the class to js if JavaScript is enabled. I prefer to use .replace("no-js","js") over the regex version because its a bit less cryptic and suits my needs.
<script>     document.documentElement.className =         document.documentElement.className.replace("no-js","js"); </script>
Prior to this technique, I would generally just apply js-dependant styles directly with JavaScript. For example:
$('#someSelector').hide(); $('.otherStuff').css({'color' : 'blue'});
With the no-js trick, this can Now be done with css:
.js #someSelector {display: none;} .otherStuff { color: blue; } .no-js .otherStuff { color: green }
This is preferable because:
  • It loads faster with no FOUC (flash of unstyled content)
  • Separation of concerns, etc...
answered Sep 13 '12 at 16:22
http://stackoverflow.com/questions/6724515/what-is-the-purpose-of-the-html-no-js-class


23 мая 2012 в 04:12

Modernizr: практическое применение 
tutorial recovery mode

Modernizr — это JavaScript-библиотека, которая узнаёт, что из HTML5 и CSS3 умеет браузер пользователя. Определяя возможности браузера, разработчик может сделать откат некоторых функций для старых версий браузеров. Создатели Modernizr называют такую проверку feature detection, и это гораздо эффективнее, чем просто определить браузер, его версию и ОС.

Я был премного удивлён факту отсутствия развёрнутой статьи об этой JS-библиотеке (анонс не в счёт). Статья составлена из перевода официальной документации проекта, переводов нескольких статей и собственных дополнений. ..

http://habrahabr.ru/post/144352/

October 2025

S M T W T F S
   1234
567891011
12131415161718
19202122 232425
262728293031 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 3rd, 2026 07:39 am
Powered by Dreamwidth Studios