PHP和组合检测浏览器中启用的JavaScript
作者:互联网
它是否正确?如果没有,那么正确的语法是什么
我是php的新手因此想学习.
<?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');
}
?>
或者有更好的方法来处理这个问题吗?
解决方法:
< 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>
Modernizr的
处理javascript检测(&功能)的更好方法是使用Modernizr:http://modernizr.com
看看这个问题:What is the purpose of the HTML “no-js” class?
一个基本的例子(没有Modernizr)
您可以在页面加载时将类no-js添加到< body>标签.然后当页面加载并且如果启用了javascript,你可以用js替换no-js,如下所示:
// 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如何工作的一个非常基本的例子.我强烈建议只使用它.
标签:javascript,php,noscript 来源: https://codeday.me/bug/20190901/1782936.html