简体   繁体   English

为什么里面的一切 <noscript> 启用Javascript后,标签不会被忽略吗?

[英]Why everything inside the <noscript> tag isn't ignored when Javascript is enabled?

Am working on an PHP application that involves multiple plans a user can select. 我正在处理一个涉及用户可以选择的多个计划的PHP应用程序。 On my Manage Plan page, whenever user selects any plan from a menu a ajax call is sent that calculates plan fee based on database values and displays it to the user. 在我的“管理计划”页面上,每当用户从菜单中选择任何计划时,都会发送ajax调用,该调用将基于数据库值计算计划费用并将其显示给用户。 That works fine. 很好 My only problem is when Javascript is disabled everything gets messed up. 我唯一的问题是,当禁用Javascript时,所有内容都会混乱。 Not only the plan fee doesn't show up (which is obvious) but when user clicks Submit button, the plan gets changed (ideally user should be taken to a checkout page) 不仅不会显示计划费用(显而易见),而且当用户单击“提交”按钮时,计划也会更改(理想情况下,应将用户带到结帐页面)

Anyways, I tried putting something like this at the top of the page. 无论如何,我试图在页面顶部放置这样的内容。

<noscript>
Javascript is disabled. Please enable it to continue.
<?php exit(); ?>
</noscript>

So when Javascript is disabled it shows a message and stops right there. 因此,当禁用Javascript时,它会显示一条消息,并在那里停止。 But when Javascript is enabled the PHP code in <noscript> tag too gets exceuted and stops the page from running. 但是当启用Javascript时, <noscript>标记中的PHP代码也会被执行并停止页面运行。

I am wondering when Javascript is enabled why everything inside the tag isn't ignored? 我想知道当启用Javascript时,为什么不忽略标记内的所有内容?

PHP is rendered on the server, not on the client. PHP是在服务器上呈现的,而不是在客户端上呈现的。 the <noscript> is only regarded or disregarded by the browser. <noscript>仅被浏览器视为或忽略。 the server has no knowledge what the client will do with this tag. 服务器不知道客户端将如何使用此标记。 even HTML comments containing PHP will be executed by the page. 页面甚至会包含包含PHP的HTML注释。

Because the page is generated in the first place on server-side and then sent to the browser in the second place. 因为该页面首先在服务器端生成,然后再发送到浏览器。 :) In addition the server doesn't know about the <noscript> -tag. :)另外,服务器不知道<noscript> -tag。

PHP gets executed server-side, not on the client. PHP在服务器端而不是在客户端执行。 Your server doesn't know anything about JavaScript, because JavaScript is executed client-side. 您的服务器对JavaScript一无所知,因为JavaScript是在客户端执行的。

The why has been answered. 为什么已经回答。

But a solution to call a php file, I found this post. 但是找到了一个调用php文件的解决方案,我发现了这篇文章。

Will depend on the context of your need. 将取决于您的需要。 worked for me. 为我工作。

<noscript><iframe src="your_php_file.php"></iframe></noscript>

Link: including php in <noscript> tag 链接: 在<noscript>标记中包含php

You can't string php and js like that. 您不能像这样字符串php和js。 php runs on your server, js runs on the clients' browsers, therefore exit() is always called. php在您的服务器上运行,js在客户端的浏览器上运行,因此始终调用exit()

The noscript tag is only interpreted by the browser rendering engine and javascript, and not by PHP on the server-side - so what is happening here is that the server is processing the PHP file before sending it to the client (the server, at this stage, doesn't care about the meaning of HTML tags) It hits exit(); noscript标记只能由浏览器呈现引擎和javascript解释,而不能由服务器端的PHP解释-因此,这里发生的情况是服务器在将PHP文件发送到客户端之前(在此,服务器正在处理PHP文件)。阶段,不在乎HTML标记的含义)它命中了exit(); and halts execution. 并停止执行。

What is then generated is sent to the browser and displayed accordingly. 然后将生成的内容发送到浏览器并相应显示。 This will be a noscript tag, the text "Javascript is disabled. Please enable it to continue." 这将是一个noscript标记,文本为“ Javascript is disabled。请启用它以继续。” and then it hits the end of what PHP produced. 然后到PHP产生的结尾。

I'm not sure what might be happening with your original situation though, sounds like there may be some similar confusion with the placement of PHP within noscript tags. 我不确定您的原始情况可能会发生什么,听起来好像在noscript标记中放置PHP可能会有类似的困惑。 Can you post an example of the original script? 您可以张贴原始脚本的示例吗?

The PHP code is (almost) always executed, no matter where it is in the page. 无论代码在页面中的什么位置,都会(几乎)始终执行PHP代码。 Your problem is not that simple, since the <script> tag is client-side code, while PHP is server - side code. 您的问题不是那么简单,因为<script>标记是客户端代码,而PHP是服务器端代码。

Server - side code is executed in the, well, server. 服务器端代码在服务器中执行。 In contrast, the client - side code is executed in the client's machine. 相反,客户端代码在客户端计算机中执行。 So PHP is not really aware of the <script> tag or whether the client can execute scripts at all. 因此,PHP并不真正了解<script>标记或客户端是否完全可以执行脚本。 It just notices <?php ... ?> sections and works on those, before sending the result to the client. 它只是注意到<?php ... ?>部分并对其进行处理,然后再将结果发送给客户端。

Your <?php exit(); ?> 您的<?php exit(); ?> <?php exit(); ?> executes on the server side before the page is served to the user. <?php exit(); ?>在将页面提供给用户之前在服务器端执行。 PHP does not care if it's in a <noscript> tag or not, therefore all further action will stop right before your </noscript> tag. PHP不在乎它是否在<noscript>标记中,因此所有进一步的操作将在您的</noscript>标记之前停止。

A possible solution is adding a disabled="disabled" attribute to your submit button, and use Javascript to remove the attribute on page load. 一种可能的解决方案是在您的提交按钮中添加一个disabled="disabled"属性,并使用Javascript在页面加载时删除该属性。 That way all users will see the whole form, but non-Javascript users will not be able to actually click the button (remember to still notify them in the <noscript> element. 这样,所有用户都将看到整个表单,但非Javascript用户将无法实际单击该按钮(请记住仍要在<noscript>元素中通知他们。

Because PHP is executed on the server, before the browser even sees the <noscript> tag. 由于PHP是在服务器上执行的,因此浏览器甚至没有看到<noscript>标记。 Javascript is client side, PHP is server side. Javascript是客户端,PHP是服务器端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP&<noscript>组合以检测浏览器中启用的JavaScript - PHP & <noscript> combination to detect enabled JavaScript in browser 为什么 select 的值没有像输入其他所有值一样显示在模态内部? - Why value of select isn't showing inside of modal like value of input everything else? 当.gitignore中的node_modules文件夹未被忽略时,为什么Git不添加/提交文件? - Why won't Git add/commit files when node_modules folder isn't ignored in .gitignore? 追加中 <noscript> 添加每个JavaScript时,将标签标记为wp_enqueue_scripts还是wp_register_script? - Appending <noscript> tag to wp_enqueue_scripts or wp_register_script when adding each javascript? 当回显它时,为什么EOF javascript代码没有出现在html中? - Why isn't this EOF javascript code appearing in the html when it is echoed? 为什么在我的 Apache PHP 构建中没有启用 XML? - Why isn't XML enabled in my Apache PHP build? 插入 <noscript> 或类似的表单标签(HTML,JavaScript) - Inserting <noscript> or similar within form tag (HTML, JavaScript) 为什么里面的html标签 <noscript> 显示为文字 - Why are the html-tags inside <noscript> shown as text 包括php in <noscript> 标签 - including php in <noscript> tag 为什么这个Javascript计算器不起作用 - Why isn't this Javascript calculator working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM