php可以检测javascript是否开启?

人气:453 发布:2022-09-22 标签: php javascript

问题描述

我有动态创建的页面。

现在我想添加ajax函数,所以我想添加if语句来改变输出。

  if(js is on){ ...  ...  echojs is on ; } else { ...  echojs is off; }   

有什么方法可以检测js是否已启用php?

或者有什么方法可以通过jquery删除/隐藏它吗?

提前致谢。

解决方案

PHP在任何浏览器操作发生之前执行,所以不,PHP无法直接检测用户是否打开或关闭了Javascript。 / p>

您必须提供更多信息,告诉您我们为您找到解决方法的工作。在PHP中,可以使用jQuery将自动设置的 $ _ SERVER ['X_HTTP_REQUESTED_WITH'] 全局变量来检测调用是通过AJAX进行的。

如果您需要在启用Javascript时显示元素,您可以先使用CSS隐藏元素并使用Javascript / jQuery启用它。反过来也是如此。

I have page which created dynamically.

Now I want to add ajax function, so I want to add if statement to change the outputs.

if(js is on){
 ...
 ... 
 echo "js is on";
}else{
...
echo "js is off";
}

Is there any way I can detect if js is on with php?

Or is there any way I can remove/hide it by jquery?

Thanks in advance.

解决方案

PHP is executed before any browser action takes place, so no, PHP cannot directly detect whether the user has Javascript on or off.

You must provide a bit more info on what you're doing for us to find you a workaround. In PHP, it is possible to detect whether the call was made via AJAX or not using the $_SERVER['X_HTTP_REQUESTED_WITH'] global variable which jQuery will set automatically.

If you need to show an element when Javascript is enabled, you can first hide the element with CSS and enable it with Javascript/jQuery. The same goes the other way around also.

494