called "no-js". This means that t...">

Javascript Replacing the class "no-js" with the class "js" does not work?

I put the class in my element <html>called "no-js". This means that the user is not using javascript (disabled or blocked). And then, in the script file I created, I want to cancel "no-js" and enter "js" so that I can determine if the user is using javascript or not through the classes. I tried using the method replace()by requesting the html tag, but it does not work. Here is what I have:

var html = document.getElementsByTagName("html")[0];
if(html.className == "no-js") {
    html.className.replace("no-js", "js"); // user has JS enabled
} 

There are no errors in the Google Chrome or Firefox console, but it does not change no-jsto js. I would use a tag <noscript>, but I like to keep it in the markup clean.

+5
source share
4

replace , .

html.className = 'js', , if.

EDIT: , <html> document.documentElement.

document.documentElement.className = 'js';

, , . if, , .

+7

, <html>, .no-js.

script , no-js .

document.documentElement.className = document.documentElement.className.replace(/\bno-js\b/g, '') + ' js ';

script , ! http://snipplr.com/view/63895/remove-nojs-class-from-html-tag-add-js-class/

+5

html.className :

var html = document.getElementsByTagName("html")[0];
if(html.className == "no-js") {
    html.className = html.className.replace("no-js", "js"); // user has JS enabled
} 
+1

(function(html) {
  html.className = html.className.replace(/\bno-js\b/, 'js')
})(document.documentElement);
<!DOCTYPE html>
<html class="no-js">
<head>
	
</body>
</html>
Hide result
+1
source

All Articles