How to detect if JavaScript is disabled?

后端 未结 30 3426
暗喜
暗喜 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:54

    I've figured out another approach using css and javascript itself.
    This is just to start tinkering with classes and ids.

    The CSS snippet:
    1. Create a css ID rule, and name it #jsDis.
    2. Use the "content" property to generate a text after the BODY element. (You can style this as you wish).
    3 Create a 2nd css ID rule and name it #jsEn, and stylize it. (for the sake of simplicity, I gave to my #jsEn rule a different background color.

    
    

    The JavaScript snippet:
    1. Create a function.
    2. Grab the BODY ID with getElementById and assign it to a variable.
    3. Using the JS function 'setAttribute', change the value of the ID attribute of the BODY element.

    
    

    The HTML part.
    1. Name the BODY element attribute with the ID of #jsDis.
    2. Add the onLoad event with the function name. (jsOn()).

    
    

    Because of the BODY tag has been given the ID of #jsDis:
    - If Javascript is enable, it will change by himself the attribute of the BODY tag.
    - If Javascript is disable, it will show the css 'content:' rule text.

    You can play around with a #wrapper container, or with any DIV that use JS.

    Hope this helps to get the idea.

提交回复
热议问题