检测是否在codeigniter中启用了JS [duplicate]

Possible Duplicate:
Check if JavaScript is enabled with PHP

I need a way to detect if javascript is enabled or disabled in the user-agent in the codeigniter framework

You cannot detect this directly on the server-side. Please take a look at this article for alternative solutions: http://www.untwistedvortex.com/detect-javascript-enabled-php/

There is no way to do a reliable detection in CodeIgniter or other framework/plain PHP.

Besides, that information is not part of the standard browser header/user-agent.

You don't get this information with the HTTP-headers. But i have a little workaround:

You can store this information in the User-Session. By default you suggest, that JS is disabled. The first delivered Page should execute a Ajax-Call to a script which tells the USER-Session, that Javascript is enabled.

Of course this has some disadvantages:

  • The first Page-Call is allways no-script
  • You don't get the information, if the User suddenly disables Javascript
  • Searching-Engines could rate your page down, if you make big differences between the script- and the noscript version

Some times it is enough to have the information, if JS is enabled or not, in the CSS. You can do it like this:

<head>
    <script type="text/javascript">
        // Jquery version
        $(function() {
            $('body').removeClass('noJs');
        });
    </script>
    <style type="text/css">
        .noJs #hello {
             display: none;
        }
    </style>
</head>
<body class="noJs">
    <div id="hello">Hello</div>
</body>