Check whether Javascript is enabled

后端 未结 9 2096
礼貌的吻别
礼貌的吻别 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.

    0 讨论(0)
  • 2020-12-29 13:56

    Whether or not javascript is enabled is only known from within the context of a browser, so the best you can do is write some browser-side code to set a flag based on if javascript is enabled or not. One possibility is do something like

    <noscript><img src="/javascript_disabled.php"></noscript>
    

    and

    // contents of javascript_disabled.php
    $_SESSION['javascript_disabled'] = 1;
    
    0 讨论(0)
  • 2020-12-29 13:58

    You can make a simple "landing page" for users without javascript AND add a javascript redirection to the javascript-enabled version of site.

    Something like this:

    ...
    <html>
    ...
    <script type="text/javascript">
     window.location.replace("/hasjs");
    </script>
    
    0 讨论(0)
  • 2020-12-29 14:00

    As the default, send out the version without javascript. There you include a little piece of javascript that redirects to the dynamic version. This will only get executed when js is enabled.

    0 讨论(0)
  • 2020-12-29 14:06
    <noscript><meta http-equiv="refresh" content="1;url=error.html"></noscript>
    

    This will redirect to an error page if script is disabled. Just replace error.html with the URL of your error page.

    0 讨论(0)
  • 2020-12-29 14:10

    Use <noscript> tags to include a meta-redirect if the page does not have JavaScript.

    <noscript> tags are called when the browser connecting to the page does not have JavaScript.

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