如何验证 xHTML 中的 noscript+meta 刷新标记?

发布于 2024-09-16 21:01:59 字数 393 浏览 6 评论 0原文

对于不支持 JavaScript 的访问者,我将他们重定向到某个页面 - “js.html”。

为此,我的所有文件中都有以下内容:

<noscript>
<meta http-equiv="Refresh" content="0;URL=js.html" />
</noscript>

当然,这在 XHTML 中无效,因为必须将 noscript 放置在 中。 但是当我将该代码放入文档正文中时,我收到另一个错误,因为元标记只能在 部分中使用,所以我陷入了无限的困境在这里循环。

有没有办法让这个验证?它在所有浏览器中都能正常工作,所以这没什么大不了的,我只是想验证我的应用程序。

For visitors that don't support JavaScript, I'm redirecting them to a certain page - "js.html".

For this, I have the following in all my files:

<noscript>
<meta http-equiv="Refresh" content="0;URL=js.html" />
</noscript>

Of course, this doesn't validate in XHTML as noscript must be placed in <body>.
But when I place that code in the body of my document, I get another error, because meta tags can only be used in the <head> section, so I'm kind of stuck in an infinite loop here.

Is there a way to make this validate? It works fine in all browsers so it's not a big deal, I just would like to validate my app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-09-23 21:01:59

您可以执行以下操作:

将元数据插入您的大脑中,但刷新时间假设为 2 秒。紧邻该位置的 SCRIPT 标记可删除元刷新。因此任何 JS 用户都不会被重定向:

<meta id="refresh" http-equiv="Refresh" content="10;URL=js.html" />
<script type="text/javascript">
    $('refresh').remove();
</script>

浏览器可能已经“提到”了元刷新。因此,您可以使用 JavaScript 在其周围编写开始和结束 HTML 注释(包括开始脚本标记以关闭第二个 document.write 的脚本标记):

<script type="text/javascript">
    document.write("<!-- ");
</script>
<meta http-equiv="Refresh" content="2;URL=js.html" />
<script type="text/javascript">
    document.write(' --><script type="text/javascript">');
</script>

我可以成功测试此注释。

只是关于我如何处理非 js 用户的提示。我有一个名为“js”的 css 类,我将其添加到仅对 javascript 用户可见的任何元素中。通过 javascript,我添加了一个 css 文件,其中包含类“js”的规则,该规则显示带有“js”类的每个元素。所有链接(功能)均经过专门设计,无需 JavaScript 即可使用,也可以在按住 CTRL 的同时单击链接的新选项卡中使用。

Here is what you could do:

Insert the meta into your head but with a refresh of let's say 2 seconds. And very next to that place a SCRIPT tag that removes that meta refresh. So any JS user will not be redirected:

<meta id="refresh" http-equiv="Refresh" content="10;URL=js.html" />
<script type="text/javascript">
    $('refresh').remove();
</script>

The browser might hav already "mentioned" the meta refresh. So you could just use JavaScript to write an opening and closing HTML comment (inlcude an opening script tag to close the script tag of the second document.write) around it:

<script type="text/javascript">
    document.write("<!-- ");
</script>
<meta http-equiv="Refresh" content="2;URL=js.html" />
<script type="text/javascript">
    document.write(' --><script type="text/javascript">');
</script>

I could successfully tested this one.

Just a hint on how I handle non-js users. I have a css class called "js" which I add to any element that should only be visible for javascript users. Through javascript I add a css file containing a rule for the class "js" that shows every element with the "js" class. All links (functions) are alo designed, that they can be used without javascript or in a new tab clicking a link while holding down CTRL.

帅冕 2024-09-23 21:01:59

我已经尝试了所有我能找到的建议,包括这个问题的答案,但没有一个有效。 Kau-Boy 对这个问题的回答 对我来说不起作用(因为它注释掉了元标记和大部分第二个代码脚本块,然后 js 在 '); 上中断,它在注释关闭后尝试解释它,即发生这种情况:

<script type="text/javascript">
    document.write("<!-- ");
</script>
<!-- <meta http-equiv="Refresh" content="2;URL=js.html" /><script type="text/javascript"> document.write(' -->
<script type="text/javascript">
    ');
</script>

我从它所做的事情中获得了灵感,并将以下似乎有效的内容放在一起:

<script type="text/javascript">
    document.write('\x3Cscript type="text/javascript">/*'); 
</script>
<meta http-equiv="Refresh" content="0;URL=js.html" />
<script type="text/javascript">/**/</script>

本质上,如果启用了 javascript,我们将获得 3 个脚本元素,其中之一是在 javascript 注释中欺骗的元标记,因此它不会重定向。如果 javascript 被禁用,它看到的只是它忽略的两个脚本元素,以及不受干扰的元元素,因此它会重定向。

注意:如果您使用 application/xhtml+xml 内容类型提供页面(我想,如果文档是 xhtml,您可能应该这样做),这将破坏浏览器中的 js,因为 write 方法通常会被禁用。

I've tried all the suggestions I could find for this, including the answers to this question, but none worked. Kau-Boy's answer to this question didn't work for me (as it comments out both the meta tag and most of the second code script block, then js breaks on '); which it tries to interpret after the comment is closed i.e. this happens:

<script type="text/javascript">
    document.write("<!-- ");
</script>
<!-- <meta http-equiv="Refresh" content="2;URL=js.html" /><script type="text/javascript"> document.write(' -->
<script type="text/javascript">
    ');
</script>

I took inspiration though from what it did do, and put together the following which seems to work:

<script type="text/javascript">
    document.write('\x3Cscript type="text/javascript">/*'); 
</script>
<meta http-equiv="Refresh" content="0;URL=js.html" />
<script type="text/javascript">/**/</script>

Essentially, if javascript is enabled, we get 3 script elements, one of which is the meta tag tricked inside a javascript comment, so it doesn't redirect. If javascript is disabled, all it sees is two script elements which it ignores, and the unmolested meta element, so it redirects.

Note: if you serve up your page with application/xhtml+xml content type (which you probably should be doing, I suppose, if the document is xhtml), this will break js in the browser, since the write method will usually be disabled.

于我来说 2024-09-23 21:01:59

正如您所发现的,这个问题在 HTML4 中无法解决。然而,目前在 HTML5 中,head 中的 noscript 是有效的,因此您可以使用 HTML5 进行验证。 (无论如何,HTML5 验证器比 HTML4 验证器要好得多)。

但需要注意的是:HTML5 有一个突出的问题 (ISSUE-117)这要求弃用 noscript,因此当 HTML5 到达最后一次调用时,noscript 在 HTML5 中可能不再有效。

As you've discovered, this problem cannot be resolved in HTML4. In HTML5 currently, however, noscript is valid in head, so you could use HTML5 for validation purposes. (The HTML5 validator is much better than the HTML4 one anyway).

One caveat though: HTML5 has an outstanding issue (ISSUE-117) which calls for deprecation of noscript, so it's possible that by the time HTML5 reaches last call, noscript will no longer be valid in HTML5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文