HTML 是否理解“if-else”?

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

Does HTML understand "if-else"?

htmllogic

提问by Sahat Yalkabov

Let's say I have Flash-version of and non-Flash version of my website. When a user comes to my website, is there any way to create the following logic:

假设我的网站有 Flash 版本和非 Flash 版本。当用户访问我的网站时,有什么方法可以创建以下逻辑:

if (user has flash-plugin installed) { load flash website }

else { load non-flash website }

While we are here. Can I do the same for the bandwidth control? Let's say a Cable customer visits my websites. He has no problem loading my 10mb fully loaded flash website with background videos. But if someone with a slow internet connection visits my website, can I skip the Flash website and re-direct that user to non-Flash website?

当我们在这里时。我可以对带宽控制做同样的事情吗?假设有线电视客户访问我的网站。他可以毫无问题地加载我的 10mb 满载 Flash 网站和背景视频。但是,如果互联网连接速度较慢的人访问我的网站,我可以跳过 Flash 网站并将该用户重定向到非 Flash 网站吗?

If there's no way to accomplish this, is there any workaround, any at all?

如果没有办法做到这一点,有什么解决方法吗?

Thanks in advance!

提前致谢!

回答by kennytm

HTML has a limited set of "if/else" test on the browser's capabilities, e.g. whether it can handle scripts (<noscript>), frames (<noframes>), etc.

HTML 对浏览器的功能进行了一组有限的“if/else”测试,例如它是否可以处理脚本 ( <noscript>)、框架 ( <noframes>) 等。

Your case on Flash-plugins can be handled by fallback content of <object>, for example:

您关于 Flash 插件的情况可以通过 的后备内容处理<object>例如

<object type="application/x-shockwave-flash" data="x.swf" width="400" height="300">
   <param name="movie" value="x.swf" />
   <p>Your browser does not support Flash etc etc etc.</p>
</object>

(See Providing alternative images if Adobe Flash isn't availablefor more alternatives.)

如果 Adob​​e Flash 不可用,请参阅提供替代图像以获取更多替代图像。)

But it's not possible to have a bandwidth control with HTML alone.

但是不可能单独使用 HTML 进行带宽控制。

回答by loentar

You must use javascript. Here is a plugin detection tool on JS.

您必须使用 javascript。这是JS上的插件检测工具。

http://www.oreillynet.com/pub/a/javascript/2001/07/20/plugin_detection.html

http://www.oreillynet.com/pub/a/javascript/2001/07/20/plugin_detection.html

 
var isFlashInstalled = detectFlash();
if (isFlashInstalled)
{
  window.location = "main_with_flash.htm";
}
else
{
  window.location = "main_no_flash.htm";
}

回答by Pekka

No, this is not doable in pure HTML. You would need the assistance of JavaScript, or maybe a server-side language like PHP, or Server Side Includes(although detecting Flash is best done using JS.)

不,这在纯 HTML 中是不可行的。你需要 JavaScript 的帮助,或者像 PHP 这样的服务器端语言,或者服务器端包含(虽然检测 Flash 最好使用 JS。)

The only "if/else" there is is for the presence of JavaScript. Anything inside a noscripttag will be shown only in browsers that don't support JavaScript or have it turned off.

唯一的“if/else”是针对 JavaScript 的存在。noscript标签内的任何内容只会在不支持 JavaScript 或已关闭 JavaScript 的浏览器中显示。

<noscript>You have JavaScript turned off!</noscript>

回答by hollsk

You would use Javascript. Here is Adobe's Flash detection instructions page: http://www.adobe.com/support/flash/how/shock/javaplugs/

你会使用Javascript。这是 Adob​​e 的 Flash 检测说明页面:http: //www.adobe.com/support/flash/how/shock/javaplugs/

Connection speed is a little more complicated and requires doing a bunch of things to test capabilities (again with javascript). Here is a proof-of-concept of this method: http://alexle.net/archives/257

连接速度有点复杂,需要做很多事情来测试功能(再次使用 javascript)。这是此方法的概念验证:http: //alexle.net/archives/257

回答by Rob

You can't do this in HTML. But you can use Javascript for it. (Something like this)

您不能在 HTML 中执行此操作。但是您可以使用 Javascript。(像这样的东西)

回答by Brad

HTML is a document format, not a language. There are a few limited things you can do based on how the browser interprets certain tags, but you should really be using Javascript.

HTML 是一种文档格式,而不是一种语言。根据浏览器如何解释某些标签,您可以做一些有限的事情,但您确实应该使用 Javascript。

回答by Antbal 0415

if-else definitely works in html files, but i don't think you can use it for what you are suggesting. Also it is actually javascript so it may not be what you are looking for.

if-else 肯定适用于 html 文件,但我认为您不能将它用于您的建议。此外,它实际上是 javascript,因此它可能不是您要查找的内容。

example of an if else statement:

if else 语句的示例:

<script>
if (A == 1) {
    B = 7
} else {
    B = 9
}
</script>

回答by Pokechu48

If you are using Python with Django or Flask, you can use Jinja. You can pass in arguments in the Python portion and use them in the HTML. The syntax for Jinja is:

如果您在 Django 或 Flask 中使用 Python,则可以使用Jinja。您可以在 Python 部分传递参数并在 HTML 中使用它们。Jinja 的语法是:

{% if condition %}
   <!--HTML here-->
{% elif condition %}
   <!--HTML here-->
{% else %}
   <!--HTML here-->
{% endif %}

As you can see, when you are done with your ifstatement, you need to use the {% endif %}statement. Jinja also allows for many other things like for loops and template inheritance. Hope this helps anyone using Python with Django or Flask!

如您所见,当您完成您的if语句时,您需要使用该{% endif %}语句。Jinja 还允许许多其他事情,例如 for 循环和模板继承。希望这可以帮助任何使用 Python 和 Django 或 Flask 的人!