Check whether Javascript is enabled

后端 未结 9 2190
礼貌的吻别
礼貌的吻别 2020-12-29 13:47

Is there a way to check whether Javascript is enabled or supported by the browser? If it\'s not supported, I would like to redirect the user to a user-friendly error page. <

9条回答
  •  时光取名叫无心
    2020-12-29 13:52

    Piece of cake!

    Encompass your entire Javascript page in one DIV.

    Overlay a second DIV of equal size that contains your non-Javascript page. Give that DIV an id and a high z-index:

    div id="hidejs" style="z-index: 200"
    

    In this second non-JS DIV, have your very first code be Javascript that sets this DIV's visibility to hidden:

    document.getElementById.("hidejs").style.visibility = "hidden";
    

    No Javascript enabled? Nothing will happen and they'll see the overlying non-Javascript page.

    Javascript enabled? The overlying non-Javascript page will be made invisible and they'll see the underlying Javascript page.

    This also lets you keep the whole page in one file, making modifications easier, rather than trying to manage two files for the same page.

提交回复
热议问题