这对吗?如果不是,正确的语法是什么
我是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>
tags
你可以使用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
看看这个问题:HTML "no-js"的目的是什么?课吗?基本示例(不含Modernizr)
您可以在页面加载时将no-js
类添加到<body>
标记。然后,当页面加载时,如果启用了javascript,您可以将no-js
替换为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如何工作的一个非常基本的例子。我强烈建议你使用它。
查看Modernizr
要做到这一点(如果你真的需要从PHP知道用户是否启用了JS):
<script>
// AJAX call to your PHP script to tell it that JS is enabled
</script>
不,这是不对的。所有的代码都是解释的,为什么要使用字符串而不是实际的布尔值?更不用说,在if语句中使用了赋值操作符而不是比较操作符。
这将不起作用,因为<noscript>
在浏览器中诱导了一个行为,并且您正在检查您的条件服务器端
如果JavaScript被禁用,这个效果非常好
HTML<noscript>
<div id="noscript-warning">This Application works best with JavaScript enabled</div>
</noscript>
CSS <style>
#noscript-warning {
font-family: sans-serif;
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 101;
text-align: center;
font-weight: bold;
font-size: 120%;
color: #fff;
background-color: #ae0000;
padding: 5px 0 5px 0;
</style>
这将不起作用,因为php是一个服务器端预处理器,除了浏览器原始请求中提供的内容外,它无法了解用户浏览器的任何信息,而浏览器原始请求中不包括当前脚本功能。
基本上,如果您想要在简单的noscript
标记之外拥有丰富的内容——它们只能添加非脚本内容,不能隐藏脚本内容——您必须:
- 将所有内容(包括普通版本和javascript版本)发送到浏览器。
- 将所有非脚本版本的html放在
div
或span
标签中,class="nocript"
。 - 将所有脚本版本的html放在
div
或span
标签中,并使用class="script"
。 - 设置css以
div.noscript{display:block}
,span.noscript{display:inline}
,.script{display:none}
开头 - 有你的javascript隐藏每个元素与
class="noscript"
与element.style.display='none'
,显示每个div
与class="script"
与element.style.display='block'
和显示每个span
与class="script"
与element.style.display='inline'
。
您还必须考虑到浏览器是一个特别恶劣的编程环境,因为用户或任何插件可以在任何时候做任何事情,例如禁用javascript。因此,您必须在PHP代码中仔细检查浏览器发送的所有内容(无论是通过表单还是AJAX),以确保它是完整的且没有损坏。
我通过在noscript标签中包装HTML注释标签来为我工作:
<noscript><!-- </noscript>
<?php
$a = 42;
echo $a;
?>
<noscript> --> </noscript>
我开始怀疑…但它管用,所以…让我知道如果有一些奇怪的情况,它不…希望这对你的问题有帮助
我认为你可以在你的脚本中这样做,在index。php脚本中,写下这段代码:
<?php
//true will be returned only if javascript is not enabled
$JSEnabled = "<noscript>true</noscript>";
//then you can do echo $JSEnabled to see the result yourself
//in a condition statement ...
if($JSEnabled) {
// ... code for JS Enabled
} else {
// ... code not JS Disabled
}
更新:
$js = null;
$js = (boolean) '<noscript>false</noscript>';
//the noscript text : false is shown when no js support detected in your browser. this value will be converted to a boolean value for testing purpose.
if($js) {
//if $js gets the no script text , that means that the browser is not supporting the js, so this line will check if $js is set to false , the else statement is fired , otherwise the if statement is fired
echo 'Your javascript is enabled<br>';
} else {
echo 'Your javascript is disabled<br>';
}
你可以使用jQuery和CSS:
<body style='display: none;'>
<!--- Content goes here --->
</body>
<script>
//jQuery
$("body").css("display","block");
//Pure JavaScript
document.body.style.display="block";
</script>
怎么样?
<noscript>
<?php
//do the include thing right here
$a=1;
?>
</noscript>
<?php
if(isset($a)){
//do nothing
}else {
//add the include u want
};
<?