PHP& < noscript>组合以检测浏览器中已启用的JavaScript [英] PHP & <noscript> combination to detect enabled JavaScript in browser

查看:142
本文介绍了PHP& < noscript>组合以检测浏览器中已启用的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是正确的吗?如果不是,那么正确的语法是什么

Is this correct? If not what is the correct syntax

我是php的新手,因此尝试学习.

I am new to php hence trying to learn.

    <?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');

        }

     ?>

还是有更好的方法来解决这个问题?

Or is there a better way to handle this?

推荐答案

<noscript>标签

您可以使用noscript标记向禁用了javascript的浏览器显示内容,也可以将其重定向到另一个页面(例如nojs-version.php).

<noscript> tags

You can use the noscript tags to display content to browsers with javascript disabled or redirect them to another page (a nojs-version.php for example).

<!-- 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>

Modernizr

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

查看以下SO问题:什么是HTML"no-js"的目的上课?

您可以在页面加载时将类no-js添加到您的<body>标记中.然后,当页面加载并且如果启用了javascript时,您可以将no-js替换为js,如下所示:

You could add the class no-js on page load to your <body> tag. Then when the page loads and if javascript is enabled, you can replace the no-js with js like so:

// 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
    }
});

上面的代码是modernizr的工作原理的非常基本的示例.我强烈建议您仅使用它.

The above code is a very basic example of how modernizr works. I would highly recommend just using that.

查看Modernizr

这篇关于PHP&amp; &lt; noscript&gt;组合以检测浏览器中已启用的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆