Why wont this code detect if javascript is blocked?

北战南征 提交于 2020-01-06 06:59:13

问题


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!");
 }

?>

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


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/16742622/why-wont-this-code-detect-if-javascript-is-blocked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!