Replace jquery with pure javascript

1

I have a jquery script to remove a class from a div automatically if browser javascript is enabled.

<script type="text/javascript">
        $("#scriptT").removeClass("noscript");
    </script>

But for this I need to import jquery (obviously), however this import is causing conflicts with other things of the site that is done with primefaces (for those who do not know the primefaces already has the jquery built-in). And you can not use the jquery of primefaces because in this particular page it does not have any primefaces component, which causes jquery not to be imported (as I'm using facelets, the import links go all in the template, consequently all the pages import ).

Anyway, can you replace this script with one in pure javascript? remembering that it has to be automatic when it initializes the site, without pressing any button.

The id #scriptT is in the div that I remove the noscript class

    
asked by anonymous 29.01.2018 / 00:30

2 answers

2

So you will not need to use onload in body . In the JS itself, it will already load the method through window.onload .

window.onload = removerClass();

function removerClass() {
   var element = document.getElementById("scriptT");
   element.classList.remove("noscript");
}
    
29.01.2018 / 00:36
2

Diego's solution is correct, but does not work in Internet Explorer 9 browsers backwards (these browsers do not support the classList property).

Just to complement, if anyone needs, the form below works in all browsers:

window.onload = removerClass();

function removerClass() {
   var element = document.getElementById("scriptT");
   var classes = element.className.split(" ");
   for(var x=0; x < classes.length; x++){
      if(classes[x] == "noscript") classes.splice(x, 1);
   }

   element.className = classes.join(" ");
}
    
29.01.2018 / 03:48