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.
, <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/
(function(html) {
html.className = html.className.replace(/\bno-js\b/, 'js')
})(document.documentElement);<!DOCTYPE html>
<html class="no-js">
<head>
</body>
</html>