How to detect if JavaScript is disabled?

后端 未结 30 3253
暗喜
暗喜 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 08:15

    In some cases, doing it backwards could be sufficient. Add a class using javascript:

    // Jquery
    $('body').addClass('js-enabled');
    
    /* CSS */
    .menu-mobile {display:none;}
    body.js-enabled .menu-mobile {display:block;}
    

    This could create maintenance issues on anything complex, but it's a simple fix for some things. Rather than trying to detect when it's not loaded, just style according to when it is loaded.

提交回复
热议问题