简体   繁体   English

显示内容是否 <noscript> 要么 <!--[if lte IE 7]> 是否满足条件注释,而不重复内容?

[英]Show content if <noscript> OR <!--[if lte IE 7]> conditional comment is met, without duplicating content?

I'm developing a Javascript-driven interactive tool that does not support IE6 or IE7. 我正在开发一个不支持IE6或IE7的Javascript驱动的交互式工具。

If someone turns up using old IE, or, using a browser with Javascript disabled, we give them some simple static content as a fallback: plain images and text. 如果有人使用旧的IE或使用禁用了Javascript的浏览器出现,我们会给他们一些简单的静态内容作为后备:纯图像和文本。

I could just do this by duplicating that static content, one copy in a <noscript> block, and one copy in a conditional comment, like this... 可以通过复制静态内容,在<noscript>块中复制一个副本和在条件注释中复制一个副本来做到这一点,就像这样...

<!--[if lte IE 7]>
<div id="some-id">
<p> Some content </p>
<img src="url.com" alt="Screenshot of awesome tool" title="This is what you're missing" />
<p> Some more content </p>
<div id="some-more"> ... etc ... </div>
</div>
<![endif]-->

<noscript><![if !(lte IE 7)]> 
<div id="some-id">
<p> Some content </p>
<img src="url.com" alt="Screenshot of awesome tool" title="This is what you're missing" />
<p> Some more content </p>
<div id="some-more"> ... etc ... </div>
</div>
<![endif]></noscript>

...but if possible, it's better to avoid duplicating content like this. ...但是如果可能的话,最好避免重复这样的内容。 No unnecessary weight, easier to update, no risk of any search engine bot interpreting hidden duplicated content as keyword spamming, etc etc. Plus, avoiding duplication is just a generally good practice. 没有不必要的负担,更容易更新,没有任何搜索引擎机器人会将隐藏的重复内容解释为垃圾邮件等的风险。此外,避免重复只是一种普遍的好习惯。

(Plus, we could lose the horrible <![if !(lte IE7)]> in the <noscript> tag... it's there in case someone has IE6 or IE7 with Javascript disabled, so they don't see content twice... I've heard of some corporate systems who have legacy custom-built systems tied to old versions of IE doing this as a horrible way to make old IE slightly more secure...) (另外,在<noscript>标记中,我们可能会丢掉可怕的<![if !(lte IE7)]> 。。。如果有人禁用了Javascript的IE6或IE7,那么它们就在那里了,因此他们不会两次看到内容。 ..我听说有些公司系统具有将旧版IE与旧版IE捆绑在一起的旧式定制系统,这是使旧版IE更加安全的一种可怕方法...)

If I wrap the conditional content in the <noscript> , or vica versa, it will be 'AND' logic - only shown when both conditions are satisfied (IE6 / 7 with Javascript disabled). 如果将条件内容包装在<noscript> ,反之亦然,则将是“ AND”逻辑-仅在两个条件都满足时显示(禁用Javascript的IE6 / 7)。

Is there any way to make it work by 'OR' logic - so there is one block of content, and that one block of content is shown if no Javascript, OR if IE less than version 7? 是否有任何方法可以使它按“或”逻辑工作-所以有一个内容块,如果没有Javascript IE小于7,则显示一个内容块? Or some clever simple reliable way using Javascript to switch the <noscript> on if the browser has Javascript enabled and is an old version of IE (ideally, without waiting for document ready)? 或者如果浏览器启用了Java并且是IE的旧版本(理想情况下,无需等待文档就绪),则可以使用Java巧妙的简单可靠方法来打开<noscript>吗?

Since I'm already using conditional comments, strict standards compliance isn't a big deal, eg if there was a way that involved script in an attribute of the <noscript> tag, that would be fine. 由于我已经在使用条件注释,因此严格遵守标准并不是什么大问题,例如,如果在<noscript>标记的属性中涉及脚本的话,那会很好。 jQuery syntax is fine, too. jQuery语法也很好。

A simple, clean way to offer one block of fallback content to old IE or noscript browsers would be a very useful thing. 向旧IE或noscript浏览器提供一个后备内容块的简单,干净的方法将是非常有用的。

I think I've cracked it without needing any javascript (and, using only valid HTML!), after idley browsing to this question which has an answer showcasing the <!--[if !(IE)]><!--> Anyone but IE <!--<![endif]--> trick: 我想我在闲逛到这个问题并显示了<!--[if !(IE)]><!--> Anyone but IE <!--<![endif]-->的答案之后,不需要任何JavaScript(并且仅使用有效的HTML!)来破解了它<!--[if !(IE)]><!--> Anyone but IE <!--<![endif]-->技巧:

<!--[if gt IE 7)]><!--> <noscript> <!--<![endif]-->
<div> Content here </div>
<!--[if gt IE 7)]><!--> </noscript> <!--<![endif]-->

The noscript tags, therefore the noscript condition, are only read if it's not old IE. Noscript标记(即noscript条件)仅在不是旧版IE的情况下才能读取。 So old IE shows the content regardless of whether javscript is active, and non-old-IE only shows the content if the noscript condition is met. 因此,旧的IE会显示内容,而不管javscript是否处于活动状态;而非旧的IE仅在满足noscript条件的情况下才显示内容。


To clarify... 澄清...

Non-IE browsers skip over the comments, reading it as: 非IE浏览器会跳过注释,将其读取为:

<!-- blah blah --> <noscript> <!-- blah blah -->
<div> Content here </div>
<!-- blah blah --> </noscript> <!-- blah blah -->

IE 8+ sees the conditional comments, meets the criteria, and reads everything. IE 8+可以查看条件注释,符合条件并读取所有内容。 This immediately closes the comments and shows the <noscript> tags: 这将立即关闭注释并显示<noscript>标记:

<!-- --> <noscript> <!-- -->
<div> Content here </div>
<!-- --> </noscript> <!-- -->

Old IE sees the conditional comments, fails the criteria, and so skips everything until it hits the [endif]. 旧版IE看到条件注释,不符合条件,因此跳过所有内容,直到到达[endif]。 With no <noscipt> tag, it always shows the fallback content: 没有<noscipt>标签,它将始终显示后备内容:

<!--[STOP READING]...[START READING]-->
<div> Content here </div>
<!--[STOP READING]...[START READING]-->

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM