这是对的吗?如果不是正确的语法
我是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');
}
?>
或者有更好的方法来解决这个问题吗?
答案 0 :(得分: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>
处理javascript检测(&amp;功能)的更好方法是使用Modernizr:http://modernizr.com
查看此SO问题:What is the purpose of the HTML "no-js" class?
您可以在页面加载时将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如何工作的一个非常基本的例子。我强烈建议你使用它。
答案 1 :(得分:3)
要实现这一目标(如果你真的需要从PHP知道用户是否启用了JS):
<script>
// AJAX call to your PHP script to tell it that JS is enabled
</script>
答案 2 :(得分:0)
不,这不正确。所有代码都被解释,为什么你使用字符串文字而不是实际的布尔值?更不用说,你在if语句中使用赋值运算符而不是比较运算符。
答案 3 :(得分:0)
它无效,因为<noscript>
会在浏览器中引发行为,而您正在检查您的条件服务器端。
答案 4 :(得分:0)
如果禁用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>
答案 5 :(得分:0)
它不起作用,因为php是一个服务器端预处理器,除了浏览器的原始请求中提供的内容之外,它无法了解用户的浏览器,其中不包括其当前的脚本功能。
基本上,如果你想拥有超出简单noscript
标签的丰富内容 - 他们只能添加非脚本内容,而不是隐藏脚本内容 - 你必须:
div
或span
标签中class="nocript"
。div
或span
标签中class="script"
。div.noscript{display:block}
,span.noscript{display:inline}
,.script{display:none}
开头。class="noscript"
element.style.display='none'
隐藏每个元素,使用div
class="script"
显示每个element.style.display='block'
并使用span
显示每个class="script"
element.style.display='inline'
与{{1}}。您还必须考虑浏览器是一个特别恶劣的编程环境,因为用户或任何插件可以随时执行任何操作,例如禁用javascript。因此,您必须仔细检查浏览器在PHP代码中发送的所有内容,无论是通过表单还是AJAX,以确保它完整且没有损坏。
答案 6 :(得分:-1)
我通过在noscript标记中包装HTML注释标记来为我工作:
<noscript><!-- </noscript>
<?php
$a = 42;
echo $a;
?>
<noscript> --> </noscript>
我有疑虑进入它......但是它有效,所以......如果有一些奇怪的情况,请告诉我它不会...希望这有助于你的问题:)
答案 7 :(得分:-1)
我认为您可以在脚本中执行此操作,在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>';
}
答案 8 :(得分:-2)
你可以用jQuery和CSS做到这一点:
<body style='display: none;'>
<!--- Content goes here --->
</body>
<script>
//jQuery
$("body").css("display","block");
//Pure JavaScript
document.body.style.display="block";
</script>
答案 9 :(得分:-6)
<noscript>
<?php
//do the include thing right here
$a=1;
?>
</noscript>
<?php
if(isset($a)){
//do nothing
}else {
//add the include u want
};
<?