PHP&<noscript>组合以检测浏览器中启用的JavaScript

Vik*_*Rao 3 javascript php noscript

它是否正确?如果没有,那么正确的语法是什么

我是新手,因此想学习.

    <?php
    // Check browser for JavaScript support

        $jsSupport='true'; ?>

        <noscript><?php $jsSupport='false'; ?></noscript>

        <?php
        if ($jsSupport == 'false') {

        include ('no-script-layout.php');

        } else {

        include ('regular-layout.php');

        }

     ?>
Run Code Online (Sandbox Code Playgroud)

或者有更好的方法来处理这个问题吗?

Ani*_*nil 12

<noscript> 标签

您可以使用noscript标签向禁用javascript的浏览器显示内容,或将其重定向到其他页面(nojs-version.php例如).

<!-- Redirect to another page (for no-js support) (place it in your <head>) -->
<noscript><meta http-equiv="refresh" content="0;url=nojs-version.php"></noscript>    

<!-- Show a message -->
<noscript>You don't have javascript enabled! Please download Google Chrome!</noscript>
Run Code Online (Sandbox Code Playgroud)

Modernizr的

处理javascript检测(和功能)的更好方法是使用Modernizr:http://modernizr.com

看看这个问题:HTML"no-js"类的目的是什么?

一个基本的例子(没有Modernizr)

您可以将no-js页面加载上的类添加到<body>标记中.然后在页面加载时,如果启用JavaScript,您可以替换no-jsjs像这样:

// When the DOM is ready & loaded, do this..
$(document).ready(function(){
    // Remove the `no-js` and add the `js` (because JS is enabled (we're using it!)
    $('body').removeClass('no-js').addClass('js');

    // Assign it to a var so you don't traverse the DOM unnecessarily.
    var useJS = $('body').hasClass('js');
    if(useJS){
        // JS Enabled
    }
});
Run Code Online (Sandbox Code Playgroud)

上面的代码是modernizr如何工作的一个非常基本的例子.我强烈建议只使用它.

看看Modernizr