How to hide parts of HTML when JavaScript is disabled?

后端 未结 8 2093
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 06:10

I\'m trying to accommodate users without JavaScript. (By the way, is it worth the effort nowadays?)

In one HTML file I\'d like part of it execute if scripts are on

8条回答
  •  清酒与你
    2021-02-07 06:38

    If the content only makes sense when JavaScript is enabled, then it should be inserted by the JavaScript code directly rather than being rendered. This could either be done by simply having HTML templates as strings within your JavaScript code, or using Ajax if the HTML is more complex.

    As mentioned by Reinis I. this is the idea of Progressive Enhancement.

    Regarding the CSS techniques of using a class name on the body tag, I would advise doing this the other way around and adding a 'js-enabled' class to the body tag with JavaScript that would alter the page CSS. This fits in with my above comment about keeping all initial HTML 'non-JavaScript friendly'.

提交回复
热议问题