检查 PHP 是否启用了 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4454551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:27:56  来源:igfitidea点击:

Check if JavaScript is enabled with PHP

phpjavascript

提问by Will

Is there a way to check if JavaScript is enabled with PHP? If so, how?

有没有办法检查 PHP 是否启用了 JavaScript?如果是这样,如何?

采纳答案by Waleed Amjad

No, that is not possible, because PHP is a server side language, it does not access the client's browser in any way or form (the client requests from the PHP server).

不,这是不可能的,因为 PHP 是一种服务器端语言,它不会以任何方式或形式(客户端从 PHP 服务器请求)访问客户端的浏览器。

The client may provide some meta info through HTTP headers, but they don't necessarily tell you whether the user has JavaScript enabled or not and you can't rely on them anyway,

客户端可能会通过 HTTP 标头提供一些元信息,但它们不一定会告诉您用户是否启用了 JavaScript,无论如何您都不能依赖它们,

回答by ozzy432836

perhaps a more simple option...

也许是一个更简单的选择......

<html>
<head>
 <noscript>
   This page needs JavaScript activated to work. 
   <style>div { display:none; }</style>
 </noscript>
</head>
<body>
<div>
my content
</div>
</body>
</html>

回答by Chris Thompson

Technically no because as the other answers have said, PHP is strictly server-side, but you could do this...

技术上没有,因为正如其他答案所说,PHP 是严格的服务器端,但你可以这样做......

In the PHP page on the server, output (a lot of HTML has been deleted for brevity)

在服务器端的PHP页面中,输出(为简洁起见删除了很多HTML)

<html>
   <head>
      <script type="text/javascript" src="jquery1.4.4.js"></script>
      <script type="text/javascript">
        $(document).ready(function(){
           $.get("myPage.php");
         });
      </script>
   </head>
 </html>

Then in myPage.phpset a session variable to indicate the client supports JS

然后在myPage.php设置一个会话变量来表明客户端支持JS

<?php
   session_start();
   $_SESSION['js'] = true;
?>

But really, just use <script></script><noscript></noscript>tags, much, much less effort...

但实际上,只需使用<script></script><noscript></noscript>标签,省力得多……

回答by Salman Akhlaqi

//Here is a solution: //it works perfect

//这是一个解决方案://它完美无缺

<?php

if(!isset($_SESSION['js'])||$_SESSION['js']==""){
  echo "<noscript><meta http-equiv='refresh' content='0;url=/get-javascript-status.php&js=0'> </noscript>";
   $js = true;

 }elseif(isset($_SESSION['js'])&& $_SESSION['js']=="0"){
   $js = false;
   $_SESSION['js']="";

 }elseif(isset($_SESSION['js'])&& $_SESSION['js']=="1"){
   $js = true;
   $_SESSION['js']="";
}

if ($js) {
    echo 'Javascript is enabled';
 } else {
    echo 'Javascript is disabled';
}

?>

//And then inside get-javascript-status.php :

//然后在 get-javascript-status.php 中:

$_SESSION['js'] = isset($_GET['js'])&&$_GET['js']=="0" ? "0":"1";
header('location: /');

回答by Juan Mendes

You can't tell if a browser has JS enabled, but you can tell if the browser supports JS http://php.net/manual/en/function.get-browser.php

无法判断浏览器是否启用了 JS,但可以判断浏览器是否支持 JS http://php.net/manual/en/function.get-browser.php

$js_capable = get_browser(null, true)=>javascript == 1

$js_capable = get_browser(null, true)=>javascript == 1

Having said this, that's probably not of much use. You should reconsider detecting JS from PHP. There should be no need for it if you use progressive enhancement, meaning that JS only adds functionality to what's already on the page.

说了这么多,估计也没啥用。您应该重新考虑从 PHP 检测 JS。如果您使用渐进增强,则不需要它,这意味着 JS 只会向页面上已有的内容添加功能。

回答by Andy Braham

Here is a small include I made up that I have on top of my pages to detect if js is enabled. Hope this helps out...

这是我在页面顶部编写的一个小包含,用于检测是否启用了 js。希望这有助于...

<?php
//Check if we should check for js
if ((!isset($_GET['jsEnabled']) || $_GET['jsEnabled'] == 'true') && !isset($_SERVER['HTTP_X_REQUESTED_WITH'])){

   //Check to see if we already found js enabled
   if (!isset($_SESSION['javaEnabled'])){
      //Check if we were redirected by javascript
      if (isset($_GET['jsEnabled'])){
         //Check if we have started a session
         if(session_id() == '') {
            session_start();
         }

         //Set session variable that we have js enabled
         $_SESSION['javaEnabled'] = true;
      }
      else{
         $reqUrl = $_SERVER['REQUEST_URI'];
         $paramConnector = (strpos($reqUrl, "?"))? "&" : "?";

         echo "
            <script type='text/javascript'>
               window.location = '" . $reqUrl . $paramConnector . "jsEnabled=true'
            </script>
            <noscript>
               <!-- Redirect to page and tell us that JS is not enabled -->
               <meta HTTP-EQUIV='REFRESH' content='0; " . $reqUrl . $paramConnector . "jsEnabled=false'>
            </noscript>
         ";

         //Break out and try again to check js
         exit;
      }
   }
}
?>

回答by Anish Bhut

<noscript>
    <?php if(basename($_SERVER['REQUEST_URI']) != "disable.html"){ ?>
        <meta http-equiv="Refresh" content="0;disable.html">
    <?php } ?>
</noscript>

Place above code in your header file after title tag and set appropriate like[disable.html] for redirection.

将上面的代码放在标题标签之后的头文件中,并设置适当的 like[disable.html] 以进行重定向。

回答by Gianluca Lodigiani

You can try with 2 metod:

您可以尝试使用 2 种方法:

  • setting cookies with JS and detecting them from PHP
  • creating a form with a hidden field and an empty value; and then assigning some value to it with JS, if the field gets the value – JS is ON, otherwise it's off. But the form had to be submitted first before PHP can request that hidden field's value.
  • 使用 JS 设置 cookie 并从 PHP 检测它们
  • 创建一个带有隐藏字段和空值的表单;然后使用 JS 为其分配一些值,如果该字段获得该值 - JS 为 ON,否则为 off。但是必须先提交表单,然后 PHP 才能请求隐藏字段的值。

if you want detect if JS enable enable setting before the loading of the page you can try this (I don't konw if it works):

如果你想在加载页面之前检测 JS 是否启用启用设置,你可以试试这个(我不知道它是否有效):

<?php
if (isset($_POST['jstest'])) {
  $nojs = FALSE;
  } else {
  // create a hidden form and submit it with javascript
  echo '<form name="jsform" id="jsform" method="post" style="display:none">';
  echo '<input name="jstest" type="text" value="true" />';
  echo '<script language="javascript">';
  echo 'document.jsform.submit();';
  echo '</script>';
  echo '</form>';
  // the variable below would be set only if the form wasn't submitted, hence JS is disabled
  $nojs = TRUE;
}
if ($nojs){
  //JS is OFF, do the PHP stuff
}
?>

there is a fine tutorial on this issue on address http://www.inspirationbit.com/php-js-detection-of-javascript-browser-settings/

在地址http://www.inspirationbit.com/php-js-detection-of-javascript-browser-settings/上有一个关于这个问题的很好的教程

回答by Adrian Soiman

<html>
<head>
<?php
  if(!isset($_REQUEST['JS'])){?>
    <noscript>
      <meta http-equiv="refresh" content="0; url='<?php echo basename($_SERVER['PHP_SELF']);?>?JS='"/>
    </noscript><?php
  }
?>
</head>
<body>    
<?php
  if(isset($_REQUEST['JS'])) echo 'JavaScript is Disabled';
  else echo 'JavaScript is Enabled';
?>
</body>
</html>

回答by Ramesh Sorathiya

before try you have to disable your browsers javascript...

在尝试之前,您必须禁用浏览器 javascript...

after then

然后

Try This code :

试试这个代码:

<html>
<head>
<noscript><meta http-equiv="refresh"content="0; url=script-disabled.html">
</noscript>
<h1>congrats ! Your Browser Have Java Script Enabled </h1>
</head>
</html>

Write something in script-disabled.html

在 script-disabled.html 中写一些东西

its work

这是工作