How to detect if JavaScript is disabled?

后端 未结 30 3449
暗喜
暗喜 2020-11-21 07:37

There was a post this morning asking about how many people disable JavaScript. Then I began to wonder what techniques might be used to determine if the user has it disabled.

30条回答
  •  野性不改
    2020-11-21 07:57

    Use a .no-js class on the body and create non javascript styles based on .no-js parent class. If javascript is disabled you will get all the non javascript styles, if there is JS support the .no-js class will be replaced giving you all the styles as usual.

     document.body.className = document.body.className.replace("no-js","js");
    

    trick used in HTML5 boilerplate http://html5boilerplate.com/ through modernizr but you can use one line of javascript to replace the classes

    noscript tags are okay but why have extra stuff in your html when it can be done with css

提交回复
热议问题