Html 如果元标记存在于文档正文中会发生什么?

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

What happens if the meta tags are present in the document body?

htmltemplatesasp-classicseometa-tags

提问by Salman A

I am working on a ASP application and the code, template and files are organized in a way that does not allow me to alter anything outside the body tag. So I am thinking about inserting the meta tags inside the body -- like this:

我正在开发一个 ASP 应用程序,代码、模板和文件的组织方式不允许我更改 body 标记之外的任何内容。所以我正在考虑在正文中插入元标记——像这样:

<!-- FEW ASP INCLUDES -->
<html>
    <head>
    <!-- FALLBACK TITLE AND DESCRIPTION -->
    <title>Default Title</title>
    <meta name="description" content="Default Description">
</head>
<body>
    <!-- SOME HTML MARKUP -->
    <div class="dynamic-content">
        <!-- InstanceBeginEditable name="dynamic-content" -->
        <!-- THIS IS WHERE I CAN WRITE ASP CODE -->
        <title><%= Page.Meta.GetTitle( yada, yada ) %></title>
        <meta name="description" content="<%= Page.Meta.GetDescription( yada, yada ) %>">
        <!-- InstanceEndEditable -->
    </div>
    <!-- SOME MORE HTML MARKUP -->
</body>
</html>

I am wondering how good it is to put meta tags inside the body of an HTML document. How does it affect:

我想知道将元标记放在 HTML 文档的正文中有多好。它如何影响:

  1. search engines
  2. browsers
  1. 搜索引擎
  2. 浏览器

采纳答案by kangax

This is of course invalidas per HTML4.01. META tags are only allowed within HEAD(just like, say, TITLE) so by putting it into a BODY, you're essentially creating an invalid markup.

根据 HTML4.01,这当然是无效的。META 标签只允许在 HEAD 中(就像 TITLE 一样),因此通过将其放入 BODY 中,您实际上是在创建无效标记。

From the cursory tests, it seems that some browsers (e.g. Firefox 3.5 and Safari 4) actually put these elements into HEAD when creating a document tree. This is not very surprising: browsers are known to tolerate and try to interpret all kinds of broken markup.

从粗略的测试来看,似乎有些浏览器(例如 Firefox 3.5 和 Safari 4)在创建文档树时实际上将这些元素放入 HEAD 中。这并不奇怪:众所周知,浏览器会容忍并尝试解释各种损坏的标记。

Having invalid markup is rarely a good idea. Non-standard handling by browsers might lead to various hard-to-pin rendering (and behavioral) inconsistencies. Instead of relying on browser guessing, it's best to follow a standard.

使用无效标记很少是一个好主意。浏览器的非标准处理可能会导致各种难以固定的渲染(和行为)不一致。与其依赖浏览器猜测,不如遵循一个标准。

I don't know how search engines react to such tag soup, but I wouldn't risk experimenting to find out :) Perhaps they only parse HEAD tag for certain information and will skip your BODY-contained tags altogether. Or maybe they consider these to be some malicious gambling attempts and black-list pages containing such markup. Who knows.

我不知道搜索引擎如何对这种标签汤做出反应,但我不会冒险尝试找出答案:) 也许他们只会解析 HEAD 标签以获得某些信息,并且会完全跳过包含 BODY 的标签。或者他们可能认为这些是一些恶意赌博尝试和包含此类标记的黑名单页面。谁知道。

The bottom line — avoid this whenever possible.

底线 - 尽可能避免这种情况。

回答by Michael H

The bottom line is to avoid this whenever possible when the DOCTYPE forbids it. I think this is definitely permitted in HTML5 and very useful in cases using microdata. Example: http://schema.org/Event

底线是在 DOCTYPE 禁止时尽可能避免这种情况。我认为这在 HTML5 中是绝对允许的,并且在使用微数据的情况下非常有用。示例:http: //schema.org/Event

回答by nullability

If your aim is search engine optimization, then it is probably a good idea to follow the standards and put all your meta tags in the <head>. However, as far as browser behavior goes, if you place <meta>tags into your <body>they will still function. I decided to test this using multiple <meta http-equiv="refresh"/>tags and <title>tags in an otherwise standards-compliant document.

如果您的目标是搜索引擎优化,那么遵循标准并将所有元标记放在<head>. 但是,就浏览器行为而言,如果您将<meta>标签放入您的标签中,<body>它们仍将起作用。我决定在其他符合标准的文档中使用多个<meta http-equiv="refresh"/>标签和<title>标签进行测试。

The results of my tests:

我的测试结果:

Firefox 18, Firefox 3.6, Firefox Mobile, Chrome 24, Chrome for Mobile, Opera 12, IE6, IE8, IE10:

Firefox 18、Firefox 3.6、Firefox Mobile、Chrome 24、Chrome for Mobile、Opera 12、IE6、IE8、IE10:

  • All <meta>tags in the body were processed.
  • The first <title>tag in the document was processed, even if it was in the body. Subsequent <title>tags were ignored.
  • The earliest meta refresh directive took effect, implying that both were processed.
  • <meta>正文中的所有标签都已处理。
  • <title>文档中的第一个标签被处理,即使它在正文中。后续<title>标签被忽略。
  • 最早的元刷新指令生效,意味着两者都被处理了。

IE9:

IE9:

  • Same as above, except all <title>tags in the body were ignored.
  • IE10 in IE9 Standards mode also behaves like this.
  • IE9 in IE8 Standards mode behaved the same as IE8, allowing 1 <title>tag in the body.
  • 同上,除了<title>正文中的所有标签都被忽略。
  • IE9 标准模式下的 IE10 也是如此。
  • IE8 标准模式下的 IE9 的行为与 IE8 相同,允许<title>正文中有1 个标签。

So, what happens when you use meta tags in the body? By and large, they seem to work fine. The meta tags will probably be processed, so if you can't put them in the head then I wouldn't fret too much.

那么,当您在正文中使用元标记时会发生什么?总的来说,它们似乎工作正常。元标记可能会被处理,所以如果你不能把它们放在头上,那么我不会太担心。

回答by John Lockwood

I wouldn't do it. That's not where those tags go, and the search engines might view it as spamming. If you can reorganize the master page you can always add a contentplaceholder up in the head section. I've done it trivially with:

我不会这样做。那不是这些标签的去向,搜索引擎可能会将其视为垃圾邮件。如果您可以重新组织母版页,您始终可以在头部部分添加一个内容占位符。我用以下方法轻松完成了:

<asp:ContentPlaceHolder ID="HeadTags" runat="server" />

This way you can add whatever content you like in the head section back on your page:

通过这种方式,您可以在页面的头部部分添加您喜欢的任何内容:

<asp:Content ID="Whatever" ContentPlaceHolderID="HeadTags" runat="server" >

    <meta ... >

</asp:Content>

回答by Stephen Ostermiller

Some meta tags that are meant for search engines will not be honored by the search engines in the body section of the page.

页面正文部分中的搜索引擎将不接受某些用于搜索引擎的元标记。

For example Google says that it will not honor a rel=canonical in the body of the page, but only in the head of the page. Here is what Matt Cutts from Google says:

例如,谷歌表示它不会在页面正文中使用 rel=canonical,而只在页面头部使用。 谷歌的马特·卡茨 (Matt Cutts) 说

We don't allow rel=canonical in the BODY (because as I mentioned, people would spam that),

我们不允许 rel=canonical 在 BODY 中(因为正如我提到的,人们会发送垃圾邮件),

回答by Parvinder Singh

I would say you to use it. I found meta tags inside body on various webpages, and they are on one of top 10 searches (on google). It shows, at least to me, that searh engines don't mind if you have used this approach.

我会说你使用它。我在各种网页上的 body 内发现了元标记,它们是前 10 名搜索之一(在谷歌上)。它表明,至少对我而言,搜索引擎不介意您是否使用过这种方法。

You must go ahead if there's no other way around.

如果没有其他办法,你必须继续前进。

回答by Sitaram

meta tagscan be added anywhere in the HTML.

元标记可以添加到 HTML 中的任何位置。

The web crawlers can read them but the only issueis when you have to share your pages over some application like facebook messenger, whatsapp etc.

网络爬虫可以读取它们,但唯一的问题是当您必须通过某些应用程序(例如 facebook messenger、whatsapp 等)共享您的页面时。

These applications only read meta tagspresent inside the head tag. So, the og:image, og:descriptionproperties of meta tagif placed inside body tagare not read and hence won't be displayed while sharing in such applications.

这些应用程序只读取存在于head 标签中的元标签。因此,元标记og:image、og:description属性(如果放置在body 标记内)不会被读取,因此在此类应用程序中共享时不会显示。

If its only for SEO purpose, you can add meta taganywhere but it is recommended to add inside head tagonly

如果仅用于 SEO 目的,您可以在任何地方添加元标记,但建议仅添加内部头标记

回答by Kaloyan Stamatov

I have put some meta tags into the body, but that is because of the Microdata technology. When I do not have a regular element with the info, that I need to describe the object according to the schema.org vocabulary , I set a meta tag with this content. I prefer to do it that way instead, to set an element with display:none. The way with display:none may cause more google problems instead of this, as far as I know. I agree it is not the best practice, but when I validate my document (HTML5) I pass the validation with zero errors. Also I have done this trick for a several projects and none of them has problems with google or other search engine. Even microdata improve the search engine ranking. I don't know what exactly you want to do with this meta tags into the body, because I have not saw any microdata in your code. If you need it for this approach I think it is ok, but in other cases meta elements must be in head section!

我在正文中加入了一些元标记,但那是因为微数据技术。当我没有包含信息的常规元素时,我需要根据 schema.org 词汇表来描述对象,我使用此内容设置了一个元标记。我更喜欢这样做,用 display:none 设置一个元素。据我所知, display:none 的方式可能会导致更多的谷歌问题而不是这个。我同意这不是最佳实践,但是当我验证我的文档 (HTML5) 时,我以零错误通过了验证。此外,我已经为几个项目完成了这个技巧,并且它们都没有在谷歌或其他搜索引擎上出现问题。甚至微数据也能提高搜索引擎排名。我不知道你到底想用这个元标记做什么,因为我没有在你的代码中看到任何微数据。

回答by cousbrad

Having the META Description tag in the websites is invalid markup, however is not a huge issue as search engines can regularly find the tag where ever it is. My website does this, have a look at my HTML

在网站中使用 META 描述标签是无效的标记,但这并不是一个大问题,因为搜索引擎可以定期找到标签所在的位置。我的网站是这样做的,看看我的 HTML

http://cameras.specced.co.uk/compare/268/Canon_EOS_200D

http://cameras.specced.co.uk/compare/268/Canon_EOS_200D

The META is in the website BODY however it has been indexed by google and the page meta description has been set as the click though text in google search results.

META 位于网站 BODY 中,但它已被 google 编入索引,并且页面元描述已设置为 google 搜索结果中的点击文本。