Check whether Javascript is enabled

后端 未结 9 2097
礼貌的吻别
礼貌的吻别 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 14:11

    As yet another option, you can (though it requires a second page visit) use javascript to set a cookie.

    If the cookie exists server-side (they have javascript) render the page as normal. During the absense of the cookie, you can either use a Location redirect, or render the appropriate [stripped-down] template to accommodate their lack of javascript support.

    page html

    <script type="text/javascript">
      document.cookie = 'hasJS=true';
    </script>
    

    page php

    if (isset($_COOKIE['hasJS'])){
      // normal page render
    }else{
      header('Location: http://mysite.com/index-nojs.php');
    }
    
    0 讨论(0)
  • 2020-12-29 14:11

    Yes. Make you have the latest jQuery.

    Javascript:

    $(function(){ $('#jsEnabled2').html('Yes it is') }) 
    

    PHP:

    $js - 'No'; 
    $jscheck = 'Javascript Enabled: '; 
    $jscheck .= '<span id="jsEnabled">'.$js.'</span>';
    print $jscheck;
    
    0 讨论(0)
  • 2020-12-29 14:13

    Bring the user to the error page by default, and have a javascript redirect execute in the section of the page. Have the redirect push the user to the real page.

    If they don't have javascript enabled, they won't get redirected and the rest of the page (error page) will load.

    0 讨论(0)
提交回复
热议问题