为什么这段代码无法检测到JavaScript是否被阻止? [英] Why wont this code detect if javascript is blocked?

查看:162
本文介绍了为什么这段代码无法检测到JavaScript是否被阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我创建了一个新问题来澄清我的旧问题,可以在这里找到:

Okay, I have created a new question to clarify my old one, which is available here: Check if certain text was outputted to the screen PHP Currently I have this code:

<?php
echo "

<noscript><h2>! JavaScript is not enabled!!! Features will not work !</h2></noscript>

<script type=\"text/javascript\">
    document.cookie= \"jsEnabled=true\";
</script>
";

if (isset($_COOKIE['jsEnabled'])) {
    // Javascript is enabled!
}
 else {
     die("JavaScript is not enabled!");
 }

?>

我不确定为什么这行不通!如果禁用了JS,它将杀死PHP!谢谢!

I am not sure why this wont work! It should kill the PHP if JS is disabled! Thanks!

推荐答案

JavaScript进程之后 PHP已完全放弃了页面,而不是之前并且没有共生.因此,您的PHP调用仅适用于该页面的第二个调用,而不适用于该页面的第一个调用.

JavaScript processes after PHP has fully given out the page, not before, and not in symbiosis. As such, your PHP call will only work for the second call to the page, not the first.

也就是说,如果您首先接受cookie.

That is, if you accept cookies in the first place.

如果要阻止没有JS的用户使用页面上的界面,请考虑改用纯JS生成界面.更可靠.

If you want to prevent users without JS from using the interface on a page, consider generating the interface in pure JS instead. More reliable.

这篇关于为什么这段代码无法检测到JavaScript是否被阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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