PHP & <noscript> 组合来检测浏览器中启用的 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14121743/
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-10-26 20:45:30  来源:igfitidea点击:

PHP & <noscript> combination to detect enabled JavaScript in browser

phpjavascriptnoscript

提问by Vikram Rao

Is this correct? If not what is the correct syntax

这个对吗?如果不是什么是正确的语法

I am new to php hence trying to learn.

我是 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');

        }

     ?>

Or is there a better way to handle this?

或者有没有更好的方法来处理这个问题?

回答by Anil

<noscript>tags

<noscript>标签

You can use the noscripttags to display content to browsers with javascript disabled or redirect them to another page (a nojs-version.phpfor example).

您可以使用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>

Modernizr

现代化

The better way to handle javascript detection (& feature) would be to use Modernizr: http://modernizr.com

处理 javascript 检测(和功能)的更好方法是使用 Modernizr:http: //modernizr.com

Check out this SO question: What is the purpose of the HTML "no-js" class?

看看这个问题:HTML“no-js”类的目的是什么?

A basic example (without Modernizr)

一个基本示例(没有 Modernizr)

You could add the class no-json page load to your <body>tag. Then when the page loads and if javascript is enabled, you can replace the no-jswith jslike so:

您可以no-js在页面加载时将类添加到您的<body>标签中。然后在页面加载时,如果启用JavaScript,您可以替换no-jsjs像这样:

// 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
    }
});

The above code is a very basic example of how modernizr works. I would highly recommend just using that.

上面的代码是一个非常基本的例子,说明了 Modernizr 是如何工作的。我强烈建议只使用它。

Check out Modernizr

查看 Modernizr

回答by Sorin Trimbitas

To accomplish this (if you really need to know from PHP if the user has JS enabled) :

要做到这一点(如果你真的需要从 PHP 知道用户是否启用了 JS):

<script>
// AJAX call to your PHP script to tell it that JS is enabled
</script>

回答by David Harris

No that is not correct. All code is interpreted, and why are you using string literals instead of actual booleans? And not to mention, you're using an assignment operator instead of a comparison operator in your if statement.

不,那是不正确的。所有代码都被解释,为什么你使用字符串文字而不是实际的布尔值?更不用说,您在 if 语句中使用了赋值运算符而不是比较运算符。

回答by moonwave99

It won't work, because <noscript>induces a behavior in the browser, and you are checking your condition server side.

它不会工作,因为<noscript>会在浏览器中引起行为,并且您正在检查您的条件服务器端。

回答by Ajit Singh

this works very good if the JavaScript is disabled

如果 JavaScript 被禁用,这非常有效

HTML

HTML

<noscript>
         <div id="noscript-warning">This Application works best with JavaScript enabled</div>
        </noscript>   

CSS

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>

回答by Patanjali

It will not work because php is a server-side pre-processor that cannot know anything about the user's browser other than what is provided in the browser's original request, which includes nothing about its current scripting capability.

它不会工作,因为 php 是一个服务器端预处理器,除了浏览器原始请求中提供的内容之外,它无法了解有关用户浏览器的任何信息,其中不包括有关其当前脚本编写功能的任何内容。

Basically, if you want to have rich content beyond the simplistic noscripttags -- they can only add non-scripted content, not hide scripted content -- you have to:

基本上,如果您想拥有超越简单noscript标签的丰富内容——它们只能添加非脚本内容,而不能隐藏脚本内容——您必须:

  1. Send all content -- both plain and javascript versions -- to the browser.
  2. Place all non-scripted versions of the html in divor spantags with class="nocript".
  3. Place all scripted versions of the html in divor spantags with class="script".
  4. Set css to start with div.noscript{display:block}, span.noscript{display:inline}, .script{display:none}.
  5. Have your javascript hide each element with class="noscript"with element.style.display='none', show each divwith class="script"with element.style.display='block'and show each spanwith class="script"with element.style.display='inline'.
  1. 将所有内容——包括普通版本和 javascript 版本——发送到浏览器。
  2. 将所有非脚本版本的 html 放入divspan带有class="nocript".
  3. 将 html 的所有脚本版本放入divspan标签中class="script"
  4. 将 css 设置为以div.noscript{display:block}, span.noscript{display:inline},开头.script{display:none}
  5. 让你的JavaScript的隐藏与每个元素class="noscript"element.style.display='none',显示每个div具有class="script"element.style.display='block'和显示每个spanclass="script"element.style.display='inline'

You also have to consider that a browser is an especially hostile environment to be programming into, as the user, or any plugins, can do anything, like disable javascript, at any time. Therefore, you have to double-check everything that the browser sends, whether by form or AJAX, in your PHP code to make sure it is complete and not corrupt.

您还必须考虑到浏览器是一个特别恶劣的环境,因为用户或任何插件可以随时执行任何操作,例如禁用 javascript。因此,您必须在 PHP 代码中仔细检查浏览器发送的所有内容,无论是表单还是 AJAX,以确保它完整且没有损坏。

回答by Tyler

I got it to work for me by wrapping an HTML comment tag inside the noscript tags:

我通过在 noscript 标签中包装一个 HTML 注释标签让它对我来说有效:

<noscript><!-- </noscript>
<?php 
 $a = 42;
 echo $a;
?> 
<noscript> --> </noscript>

I had my doubts going into it...but it works, so...Let me know if there's some wierd case where it doesn't...Hope this helps with your problems :)

我对此表示怀疑......但它有效,所以......如果有一些奇怪的情况它没有......希望这有助于解决你的问题:)

回答by Shabasha

I think that you can do that in your script , in a index.php script , write down this code :

我认为你可以在你的脚本中做到这一点,在一个 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 

  }

UPDATE :

更新 :

   $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>';
   }

回答by Federico Piragua

Yo can do this with jQuery and CSS:

你可以用 jQuery 和 CSS 做到这一点:

<body style='display: none;'>
<!--- Content goes here --->
</body>
<script>
//jQuery
$("body").css("display","block");
//Pure JavaScript
document.body.style.display="block";
</script>

回答by Fuad khattab

how about ?

怎么样 ?

<noscript>
<?php 
//do the include thing right here 
$a=1;
?>
</noscript>
<?php
if(isset($a)){
//do nothing
}else {
//add the include u want 
};
<?